Re: [SLUG] SED help

From: Andrew Barber (tuorum@yahoo.com)
Date: Thu Jul 21 2005 - 11:30:29 EDT


Maybe try encapsulating the variable like this:

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

I'm not a bash dude, so I can only speak to ksh, but
for the variable expansion to be performed by the
shell, the sed expression needs to be in double
quotes, not single.

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

Though sed works on the entire file, so I'm not sure
why you would want to loop over each line. And the
sed command you use will work on the entire file
anyway.

My preference if I NEED to loop over each line (like a
config file) is:

while read filename
do
     echo "$filename" | something
     some_other_thing
done < $filename list

But I would not recommend that loop for anything over
100 lines. Way too many exec's required for each line
within the loop.

Andy

--- Keith Lelacheur <themercuryman@gmail.com> wrote:

> I am having one of those days, can anyone help out
> with a probably
> very simple question? 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`
> 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. I have also tried -- sed -n '$ap'
> $filename -- but it
> just returns the "p". Another attempt was -- sed
> '$a!d' $filename --
> and it returned the whole file as well. Any help
> with passing the
> variable to the SED address would be most
> appreciated.
>
> Thanks,
>
> Keith
>
>
-----------------------------------------------------------------------
> 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 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:09:50 EDT