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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sed problem with variable

Status
Not open for further replies.

harwooddale

Programmer
Feb 7, 2003
40
GB
Hi,
I am having a problem passing a variable to the sed command.

For example:

$filename=13017105.ack

#I then want to count how many lines there are in the file,
#but need the output to be a number.

#The output looks something like:
#19 13017105.ack

#command to try and just retrieve number:
filename1=`wc -l $filename | sed 's/$filename//g'`

This doesn't work.

If I replace $filename with 13017105.ack it works perfectly!

Any Ideas

Any help appreciated


James
 
Dont use quotes around the variable

filename1=`wc -l $filename | sed s/$filename//g`

Or just use...

filename1=`wc -l < $filename`
 
#command to try and just retrieve number:
filename1=`wc -l $filename | sed &quot;s/$filename//g&quot;`

OR (isn't it what you REALLY want?)

#command to try and just retrieve number:
filename1=`wc -l < $filename`

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Perhaps...

#!/bin/sh

filename=13017105.ack
num_lines=`wc -l < $filename | sed 's/ //g'`

echo &quot;$num_lines $filename&quot;


An example of referencing shell variables in sed...

#!/bin/sh

myvar=&quot;foo&quot;
sed 's/'&quot;$myvar&quot;'/boo/g' inputfile
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top