1752310 Members
5964 Online
108786 Solutions
New Discussion юеВ

Re: 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