1830166 Members
5441 Online
109999 Solutions
New Discussion

variables in sed

 
SOLVED
Go to solution
Kris_5
Occasional Advisor

variables in sed

Hi Folks,

How do I put variables in a sed command?
for e.g.

bline_no=1
eline_no=35

sed '"$bline_no", "$eline_no"d' temp.txt >temp1.txt

Actually the requirement is I need to remove some of the lines from a text file and copy it to another file. Thanks in adv.

Kris
3 REPLIES 3
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: variables in sed

Hi:

This should get you started. The missing piece is 'eval'. The remainder is left as an exercise for the student.

#!/usr/bin/sh

bline_no=1
eline_no=35

eval sed -e '${bline_no},${eline_no}d' temp.txt > temp1.txt


Plan B) Build your sed commands as a script and use the -f arg.

Regards, Clay
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: variables in sed

Hi:

Try this:

sed "$bline_no","$eline_no"d temp.txt > temp1.txt

Regards!

...JRF...
Craig Rants
Honored Contributor

Re: variables in sed

Have you tried using a command file with the
-f option? The file would look like this

s/.cf//g
/default/d
/newname/d

The command string like this:

sed -f "commandfile" file.txt > file1.txt

I would recommend getting the O'Reily book on sed and awk. A lot of great stuff in there.

Enjoy
C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut