- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- 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
Forums
Discussions
Discussions
Discussions
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2008 03:00 AM
08-07-2008 03:00 AM
Re: A few sed questions
> Dennis: I would suggest you not use sed for something this complicated. awk is much easier to understand.
While 'sed' was not my prefereence either, I would use Perl for the second solution:
# perl -ple 'if (/^install/../^endif/) {print "newline ADDED" if /^endif/}' file
...short, sweet and can be inlined in a shell script.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2008 12:37 PM
08-13-2008 12:37 PM
Re: A few sed questions
input
*************************
instal: a b c
ifeq
whatever
endif
********************
I want to do string substition on "install:"
but only if there is a "ifeq" right underneath it... how do I do that?
thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2008 12:51 PM
08-13-2008 12:51 PM
Re: A few sed questions
Regarding your newest question, I think you need to look to Perl or 'awk', so:
# perl -nle 'if (/^ifeq/) {$hold=~s/install/NEWINSTALL/};print $hold if defined $hold;$hold=$_' file
...using this input:
# cat input
install: a b c
ifeq
whatever
endif
install: a b c
ofeq
whatever
endif
install: a b c
ifeq
whatever
endif
...the Perl script yields:
NEWINSTALL: a b c
ifeq
whatever
endif
install: a b c
ofeq
whatever
endif
NEWINSTALL: a b c
ifeq
whatever
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2008 01:01 PM
08-13-2008 01:01 PM
Re: A few sed questions
So, it can not be done in sed, but it can be done in perl right?
no problem, I can use perl in the shell script, i just want to be sure!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2008 01:02 PM
08-13-2008 01:02 PM
Re: A few sed questions
when you reply, I will submit the right point again.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2008 01:06 PM
08-13-2008 01:06 PM
Re: A few sed questions
Thanks! Glad to help!!!
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2008 01:07 PM
08-13-2008 01:07 PM
Re: A few sed questions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2008 01:11 PM
08-13-2008 01:11 PM
Re: A few sed questions
> So, it can not be done in sed, but it can be done in perl right?
Well, in fairness, I wouldn't say that it can't be done with 'sed' --- a 'sed' guru probably could, but I'm not that. A good 'sed' guide, including some very advanced handling is here:
http://www.gnu.org/software/sed/manual/sed.html
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2008 01:32 PM
08-13-2008 01:32 PM
Re: A few sed questions
OK, 'awk' looks like this:
# awk '{if (/^ifeq/) {sub("install","NEWINSTALL",hold)};if (NR>1) {print hold};hold=$0}' file
...with the same example I used for the Perl snippet.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2008 02:02 PM
08-13-2008 02:02 PM
Re: A few sed questions
You are missing the last line:
awk '
{
if (/^ifeq/) {
sub("install","NEWINSTALL",hold)
}
if (NR>1) {print hold}
hold=$0
}
END {print hold}' file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2008 02:46 PM
08-13-2008 02:46 PM
Re: A few sed questions
> Dennis: You are missing the last line
Oops, yes, indeed! Add to the Perl script, too:
END{print $hold}'
# perl -nle 'if (/^ifeq/) {$hold=~s/install/NEWINSTALL/};print $hold if defined $hold;$hold=$_;END{print $hold}' file
# awk '{if (/^ifeq/) {sub("install","NEWINSTALL",hold)};if (NR>1) print hold};hold=$0};END{print hold}' file
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2008 02:59 PM
08-13-2008 02:59 PM
Re: A few sed questions
both of you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2008 05:47 PM
08-13-2008 05:47 PM
Re: A few sed questions
awk '
BEGIN {hold=$0}
{
if (/^ifeq/) {
sub("install","NEWINSTALL",hold)
}
print hold
hold=$0
}
END {print hold}' file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2008 06:46 AM
08-14-2008 06:46 AM
Re: A few sed questions
> Dennis: You can also remove the check for NR > 1 by using BEGIN
Yes, and similarly, in my Perl code I could do:
# perl -ne 'INIT{$hold=<>};if (/^ifeq/) {$hold=~s/install/NEWINSTALL/};print $hold;$hold=$_;END{print $hold}' file
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2008 01:20 PM
08-14-2008 01:20 PM
Re: A few sed questions
All right, here's a 'sed' script for you question of August 13:
# cat sedfilter
#!/usr/bin/sed -nf
h;N
/ifeq/ {H;s/install/NEWINSTALL/;p;}
/ifeq/!p
...run as:
# sedfilter file
Regards!
...JRF...