Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SED, adding text bellow line 2

Status
Not open for further replies.

RFC1795

IS-IT--Management
Feb 13, 2003
76
US
Hi all,

I'm having a small problem which I'm struggling to find answer to. My main problem is adding two (or more) lines under a certain line in a file.

To keep it a straight forward example as possible, I have a file called "sample.txt" which contains lines as follows:

entry 1/1
entry 2/1
entry 3/1
entry 4/1
entry 5/1

#Script looks like so:
#!/bin/bash
FILE="sample.txt"
cat $FILE | sed -e '/^entry/a\
note 1
'
# End of script

Output is like this:
entry 1/1
note 1
entry 2/1
note 1
entry 3/1
note 1
entry 4/1
note 1
entry 5/1
note 1

This is fine ... however, the two problems I have are:
1- How do I make this work by introducing the new text to be added as a variable
ie If at top of script I put TXT="note 1" and make script look like this:

FILE="sample.txt"
TXT="note 1"
cat $FILE | sed -e '/^entry/a\
$TXT
'

The output comes out wrong like follows:
entry 1/1
$TXT
etc... (I've tried different quote marks without luck)

2- How do I put more than one entry in to get output like so:

entry 1/1
note 1
note 2
entry 2/1
note 1
note 2
entry 3/1
note 1
note 2

I've tried various things but not winning *sigh*

Any tips or pointers to previous posts that I'm not finding is appreciated.

Thanks... TJ
 
here comes the UUOC-police.....

Code:
FILE='sample.txt'
TXT='note 1
note 2'
sed -e "/^entry/a\
${TXT}
" ${FILE}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks Vlad,

But still having a problem .. I copied your example as a fresh start.. but I get this error:

sed: command garbled: /^entry/anote 1

Cheers... TJ
 
And this ?
sed -e "/^entry/a\\
${TXT}
" ${FILE}
Or this ?
sed -e '/^entry/a\
'"${TXT}
" ${FILE}

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Oh one more thing .. I'd like to keep the "cat $FILE | sed etc etc.." part if possible as I have a bit more going on in that line ... like grep -v's etc ... my operational $FILE is a lot more complex than what I've supplied in sample.

Cheers... TJ
 
Hi PHV,

I tried both your samples ... copied across. Both gave the same error:

sed: command garbled: note 2

Thanks... TJ
 
I'd like to keep the "cat $FILE | sed etc etc.." part if possible
Why ?
sed "sed script" $FILE | grep -v ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
actually it's this:

Code:
#!/bin/ksh

FILE='sample.txt'
TXT='note 1\
note 2'
sed -e "/^entry/a\\
${TXT}
" ${FILE}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks Vlad and PHV !!!!

You guys are brilliant ... It's working just the way I need it to. :)

Cheers... TJ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top