[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: [ask] awk - passing for loop bash variables to awk



On Sun, Sep 16, 2012 at 3:44 AM, Morning Star
<morning.star.crew@gmail.com> wrote:
> Hi guys,
> I get a difficulty to produce the desired output using awk. i want to
> use for loop bash variable as the input to the awk variable
> here is the illustrated input:
>
> line_1
> line_2
> line_3
> line_4
> line_5
> line_6
> line_7
> line_8
> line_9
> line_10
>
> here is the desired output:
> line_1
> line_2
> line_3
>
> here is what i do:
> cat input | for (( i=1;i<=3;i++ )); do gawk -v var=$i 'NR == var { print}'; done
>
> but, the result is always:
> line_1
>

$ for (( i=1;i<=3;i++ )); do gawk -v var=$i 'NR == var { print}' input ; done

You're asking awk to read lines from a file, so you need to give the
file over to awk.  The above gives you the output you're looking for.
The bash portion of your script (the loop) and the -v var portion of
your awk command are fine.

-- 
Chris


Reply to: