- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: A few sed questions
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-06-2008 01:00 PM
тАО08-06-2008 01:00 PM
for example
input
*******************
current
**********************
output
*****************
before
current
******************
I have the following
sed "/^current/i before " $file
but it didnt insert a blank line right after the string "before" thank you
Solved! Go to Solution.
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-06-2008 01:37 PM
тАО08-06-2008 01:37 PM
Re: A few sed questions
Perl is so much easier:
# perl -ple 'print "before\n" if /^current/' file
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-06-2008 01:56 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-06-2008 03:12 PM
тАО08-06-2008 03:12 PM
Re: A few sed questions
May I ask you another question? you can use sed, or any shell commands. I have been trying differerent combinations, but I can not get it right...
I want to parse the following input file. basically, I want to search for "install:", then add a new line right before "endif"
I tried the following, but "N" only read the next line, what if I am not sure how many lines, but I want to add a line right before "endif" in the "install:" block.
thanks for spending your time to educate me!
sed -e '/^install:/{
N
s,endif,new string,
#}' $file
input
******************************
install: ap_dir
some lines..
endif
preinstall: pub_dir
some lines..
endif
*******************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-06-2008 03:55 PM
тАО08-06-2008 03:55 PM
Re: A few sed questions
Try this:
# cat mysed
#!/usr/bin/sed -f
/^install/,/^endif/ {
/^endif/ {
i\
newline ADDED
}
}
...using your input:
# cat somestuff
install: ap_dir
some lines..
endif
preinstall: pub_dir
some lines..
endif
...run as:
# ./mysed somestuff
install: ap_dir
some lines..
newline ADDED
endif
preinstall: pub_dir
some lines..
endif
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-06-2008 04:10 PM
тАО08-06-2008 04:10 PM
Re: A few sed questions
thank you so much!!!
btw, do you have the sed/awk book? where can I learn this trick!!
thanks a million again!!!!!!!!
thank you!!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-06-2008 04:27 PM
тАО08-06-2008 04:27 PM
Re: A few sed questions
I'm happy that I could help.
You might try:
http://www.gnu.org/software/sed/manual/
...and O'Reilly's :
http://oreilly.com/catalog/9781565922259/
...and in various places and various forms:
http://student.northpark.edu/pemente/sed/sed1line.txt
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-06-2008 04:29 PM
тАО08-06-2008 04:29 PM
Re: A few sed questions
...and a few credits for the last solution I posted would be appreciated too :-))
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-06-2008 04:30 PM
тАО08-06-2008 04:30 PM
Re: A few sed questions
you really made my day!!!
I will check the reference sites!
but, I have searched the website, I think that your example is the best, I have not seen on like that!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-06-2008 08:13 PM
тАО08-06-2008 08:13 PM
Re: A few sed questions
(One reason for doing it, is to be able to have single quotes inside.)
But you can do the whole thing inside a shell script too:
$ sed -e '
/^current/i\
before\
' $file
>I want to parse the following input file. I want to search for "install:", then add a new line right before "endif"
I would suggest you not use sed for something this complicated. awk is much easier to understand.
awk '
/^install:/ { flag = 1 }
/^endif/ && flag {
print "some new stuff"
flag = 0
}
{ print }' $file
- Tags:
- awk