1827705 Members
2634 Online
109967 Solutions
New Discussion

A few sed questions

 
SOLVED
Go to solution
Gemini_2
Regular Advisor

A few sed questions

how do you insert a string and a **blank line** by using sed.

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
25 REPLIES 25
James R. Ferguson
Acclaimed Contributor

Re: A few sed questions

Hi:

Perl is so much easier:

# perl -ple 'print "before\n" if /^current/' file

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor
Solution

Re: A few sed questions

Hi (again) Gemini:

# cat mysed
#!/usr/bin/sed -f
/^current/{
i\
newline with blank line
i\

}

...run as :

# ./mysed file

Note that I used two (2) (i)insert operators.

Regards!

...JRF...
Gemini_2
Regular Advisor

Re: A few sed questions

Thanks for your clear explaination. I now learned how to use "i\" I see, it is one line at at time.

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
*******************
James R. Ferguson
Acclaimed Contributor

Re: A few sed questions

Hi (again):

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...
Gemini_2
Regular Advisor

Re: A few sed questions

I can not believe it! it works soo beautifully!!

thank you so much!!!

btw, do you have the sed/awk book? where can I learn this trick!!

thanks a million again!!!!!!!!

thank you!!!
James R. Ferguson
Acclaimed Contributor

Re: A few sed questions

Hi (again) Gemini:

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...
James R. Ferguson
Acclaimed Contributor

Re: A few sed questions

Hi Gemini:

...and a few credits for the last solution I posted would be appreciated too :-))

Regards!

...JRF...
Gemini_2
Regular Advisor

Re: A few sed questions

thanks a million again!

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!

Dennis Handly
Acclaimed Contributor

Re: A few sed questions

Note JRF's solution uses scripts whose interpreter is the command sed. awk also works like this.
(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
James R. Ferguson
Acclaimed Contributor

Re: A few sed questions

Hi (again) Gemini:

> 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...
Gemini_2
Regular Advisor

Re: A few sed questions

One more sed question

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.


James R. Ferguson
Acclaimed Contributor

Re: A few sed questions

Hi (gemini:

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...
Gemini_2
Regular Advisor

Re: A few sed questions

thanks for your speedy reply.

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!

Gemini_2
Regular Advisor

Re: A few sed questions

hmm...I submit 10 point, how come it become 0 point.

when you reply, I will submit the right point again.

James R. Ferguson
Acclaimed Contributor

Re: A few sed questions

Hi (again):

Thanks! Glad to help!!!

Regards!

...JRF...
Gemini_2
Regular Advisor

Re: A few sed questions

btw, if you dont mind, can you tell me how you would do it in awk?

James R. Ferguson
Acclaimed Contributor

Re: A few sed questions

Hi Gemini:

> 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...
James R. Ferguson
Acclaimed Contributor

Re: A few sed questions

Hi Gemini:

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...
Dennis Handly
Acclaimed Contributor

Re: A few sed questions

>JRF: awk looks like this:

You are missing the last line:
awk '
{
if (/^ifeq/) {
sub("install","NEWINSTALL",hold)
}
if (NR>1) {print hold}
hold=$0
}
END {print hold}' file
James R. Ferguson
Acclaimed Contributor

Re: A few sed questions

Hi (again) Gemini:

> 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...
Gemini_2
Regular Advisor

Re: A few sed questions

thanks you for your kind help!

both of you!

Dennis Handly
Acclaimed Contributor

Re: A few sed questions

You can also remove the check for NR > 1 by using BEGIN:
awk '
BEGIN {hold=$0}
{
if (/^ifeq/) {
sub("install","NEWINSTALL",hold)
}
print hold
hold=$0
}
END {print hold}' file
James R. Ferguson
Acclaimed Contributor

Re: A few sed questions

Hi (again) Gemini:

> 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...
James R. Ferguson
Acclaimed Contributor

Re: A few sed questions

Hi Gemini:

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...