Operating System - HP-UX
1824635 Members
4268 Online
109672 Solutions
New Discussion юеВ

Adding second line with sed to file

 
SOLVED
Go to solution
jerry1
Super Advisor

Adding second line with sed to file

Does anyone know how to add two lines of
text into a file(not end of file) when
replacing one line with sed.

example:

Look for string in file of "#end":

#end

somewhare in file and add "newline" string plus "#end" string afterwords:

newline
#end


It is of course easy to use:

sed s/string1/strings2/

But I just can't get the new line of text added afterwords.

sed s/strings1/strings2\nstring1/ ????



10 REPLIES 10
Mel Burslan
Honored Contributor
Solution

Re: Adding second line with sed to file

I found myself in the same conundrum more than once in my life and devised a methodology without getting too much into cryptic sed commands.

I tweak this little script everytime I use it as my needs change, but you can get the basic idea. chop up the file, insert your text and reconstruct it.

Here it goes and hope it helps.


FILE=File_to_be_edited
PATTERN="whatever you wish to find"

# find your line number which contains the pattern
findline=`grep -n "${PATTERN}" $FILE|cut -d: -f1`

# cut and paste whatever is before this line into a header section
sed -e "${findline},\$d" ${FILE} > /tmp/file_header

# cut and paste whatever is after this line into a footer section
let f_minus_one=${findline}-1
sed -e "1,${f_minus_one}d" ${FILE} > /tmp/file_footer

# reconstruct your FILE
#
# put the header section first
cat /tmp/file_header > ${FILE}

# add your insertions
echo "my inserted line" >> ${FILE}
echo "another inserted line" >> ${FILE}

# finish off with putting whatever is in the footer section
cat /tmp/file_footer >> ${FILE}
________________________________
UNIX because I majored in cryptology...
curt larson_1
Honored Contributor

Re: Adding second line with sed to file

how about something like:

sed '/s1/{h;s/s1/s2/;G;}'
Vibhor Kumar Agarwal
Esteemed Contributor

Re: Adding second line with sed to file

Not sure of the syntax but it works:

sed -e 's/string1/&string2/g'
Vibhor Kumar Agarwal
Muthukumar_5
Honored Contributor

Re: Adding second line with sed to file

Is it working as,

sed -e 's/#end/newline\
#end/g'

Else you can go with awk as,

awk '{ if ( $0 ~= "#end" ) { printf "newline\n"$0"\n" }}'

hth.
Easy to suggest when don't know about the problem!
jerry1
Super Advisor

Re: Adding second line with sed to file

Mel, thanks. Good enough.

I could not however get the others to
work.


# sed "s/#end/{h;s/#end/test/;G;}" rules.test
sed: command garbled: s/#end/{h;s/#end/test/;G;}


Not sure if the syntax is right below. $0 - ??

# awk '{ if ($0 ~= "#end" ) { printf "newline\n"$0"\n" }}' rules.test
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1


Does not give me a new line for the string
"test" but it seems to be close. Seems like
you could put some kind of switch in between
& and the string "test".

sed "s/#end/&test/g" rules.test

#endtest


------------------------------------
I tried this one already.

# sed -e "s/#end/test\
newline/g" rules.test

testnewline


curt larson_1
Honored Contributor

Re: Adding second line with sed to file

hello Jerry,

in regards to:

I could not however get the others to work.
# sed "s/#end/{h;s/#end/test/;G;}" rules.test
sed: command garbled: s/#end/{h;s/#end/test/;G;}

you have the syntax wrong; it should be:

sed '/#end/{h;s/#end/test/;G;}' rules.test

not:
sed "s/#end/{h;s/#end/test/;G;}"
aaaaa^, there is no "s" at the begining.
curt larson_1
Honored Contributor

Re: Adding second line with sed to file

and the awk command would work better as:

awk '{ if ( $0 ~= "#end" ) {
printf("newline\n%s\n",$0);}}'

or

awk '/#end/ {printf "newline\n%s\n",$0);}'
Hein van den Heuvel
Honored Contributor

Re: Adding second line with sed to file

Jerry,

The sed line Muthukumar showed works interactively, but it hard to do in a script.
You need that new newline character after the 'text for the new line'.

The awk suggestion needs a little tweak.
This works for me:
awk '/^\#end/{print "newline"} {print}'

demo below.

Hein.

#cat x
aap
end #end
noot
#end
#
# awk '/^\#end/{print "newline"} {print}' x
aap
end #end
noot
newline
#end
$ sed -e 's/#end/newline2\
> #end/g' x
aap
end newline2
#end
noot
newline2
#end

Rory R Hammond
Trusted Contributor

Re: Adding second line with sed to file

I know how to cheat

Assume your data does have a Carriage Return
(^M, control M)

sed 's/#end/ XXXX ^M #end/g' |tr "\r" "\n"

You can insert a ^M, using vi by
Control V and then Control M.

If you know your data NEVER has a certain character like a ~, etc you could use that instead of a control character.

Rory
There are a 100 ways to do things and 97 of them are right
jerry1
Super Advisor

Re: Adding second line with sed to file

My appologies Curt. Habbit of always
typing sed s/

They both work.
Curt you had a typo though in:

awk '/#end/ {printf "newline\n%s\n",$0);}'

No ")" after 0 and it works.

Hein van den's works also.

Thanks Guys!