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 problem in script + extracting lines

Status
Not open for further replies.

LGJ

Programmer
Mar 7, 2003
50
GB
Hi all,

I have the following problem which I'm sure is something to do with quoting issues?

I want to get the number of lines from a file > num_lines
and use this in a sed command to extract the contents to a new file e.g:
=======================================
num_lines=`cat ${PINLOG} | wc -l`
echo "number lines = " ${num_lines}
sed -n "1,${num_lines}p" cm.pinlog > file_temp
=======================================

This gives me the following output:

number lines = 2171
sed: command garbled: 1, 2171p


Any ideas and useful info on where I'm going wrong would be great. I have a real problem with when to use " or '

Thanks
Lee
 
num_lines=`cat ${PINLOG} | wc -l`
echo "number lines = " ${num_lines}
sed -n "1,${num_lines}p" cm.pinlog > file_temp

Provided $PINLOG=cm.pinlog, why not simply do this ?
cat $PINLOG > file_temp
Anyway, you may consider something like this:
eval num_lines=\`expr 0 + `wc -l <${PINLOG}`\`
or, if you're using ksh or similar shell:
typeset -i num_lines=$(wc -l <${PINLOG})

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
lol.

I knew someone would say to just do cat $PINLOG > file_temp

I am doing this because for some weird reason when I grep the original file (ie cm.pinlog) for say E at the start of the line then some lines go missing but when I cut the lines to new file the grep works as it should???

Thanks again, what you showed worked.
 
LGJ: the main problem are the spaces in front of $num_lines,
wc's output is formatted.
the chipest way is to use an other tool for computing lines,
try:
num_lines=`grep -c '.*' ${PINLOG}`
echo "number lines = " ${num_lines}
sed -n "1,${num_lines}p" cm.pinlog > file_temp


:) guggach
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top