Re: [SLUG] SED help

From: Eben King (eben1@tampabay.rr.com)
Date: Thu Jul 21 2005 - 14:53:40 EDT


#include <std_disclaimer.h>

On Thu, 21 Jul 2005, Keith Lelacheur wrote:

> I have a script to move through each line of a file and do some parsing
> with a combiantion of sed and awk. I am having trouble passing a variable
> into the address portion of the sed command though. The basic outline is:
>
>
> ____________________________
> #!/bin/bash
> filename=$1
> filelines=`wc-l $filename`

My wc acts like so:

[eben@pc tmp]$ foo=`wc -l watch-receipt.htm`
[eben@pc tmp]$ echo "$foo"
    216 watch-receipt.htm

Something like this might work:

[eben@pc tmp]$ foo=`wc -l < watch-receipt.htm`
[eben@pc tmp]$ echo "$foo"
    216

> for ((a=1; a <= filelines ; a++))
> do
> sed '$aq;d' $filename
> blah,
> blah,
> blah,
> done
> _____________________________
>
>
> the problem with the -- sed '$aq;d' $filename -- syntax is that it
> process evey line of the file not the line number specified by the
> loop counter.

$a is in single quotes. Variables aren't expanded there -- what sed gets is
not

1q;d somefile
2q;d somefile
3q;d somefile
...

but

$aq;d somefile
$aq;d somefile
$aq;d somefile
...

; isn't parsed when in double quotes (but is when not), and it's
conceiveable that you'd try to use a filename with #, ;, space and so on,
and $aq will be expanded to whatever happens to be in "aq" (or nothing if
that's the case), so replacing that line with

       sed "${a}q;d" "$filename"

should work. Also, comp.unix.shell is great.

-- 
"Never go off on tangents, which are lines that intersect a curve at only
 one point and were discovered by Euclid, who lived in the 6th century,
 which was an era dominated by the Goths, who lived in what we now know 
 as Poland." - Unknown from Nov. 1998 issue of Infosystems Executive.

----------------------------------------------------------------------- This list is provided as an unmoderated internet service by Networked Knowledge Systems (NKS). Views and opinions expressed in messages posted are those of the author and do not necessarily reflect the official policy or position of NKS or any of its employees.



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 18:11:46 EDT