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

2012-09-17 Thread Morning Star
On Mon, Sep 17, 2012 at 7:57 AM, Cam Hutchison  wrote:
> When awk runs, it reads its input until EOF. In your loop, the first run
> of awk is consuming all the input from stdin (cat input) and printing
> the first line. For the subsequent iterations through the loop, awk no
> longer has anything to read - it gets EOF when attempting to read the first
> line. This means the script never matches anything and will not print
> anything.

ah, i see. i understand now. it's more clear now to me. thanks, Cam
Hutchison. :)

> You will need to have awk re-read the input each time:
>
> for (( i=1;i<=3i++ )) ; do
>   cat input | gawk -v var=$i 'NR == var { print; exit }'
> done

you forget to put the semicolon before the increment.  for (( i=1;i<=3;i++ ))
:)

> I've added an "exit" to the awk script since after the action is executed,
> there is clearly no more work to be done for the rest of the file, so it
> make sense to terminate early.

so it's more effective with exit. hmm.. i see. thanks again.

> "cat input | " is not needed - it's a useless use of cat, but I don't
> know if you have it here as a representation of a more complex pipeline
> that is not relevant to your question. If you are literally using
> "cat input | ", you can replace it with either:
>   gawk -v var=$1 '...' input
>
> (i've remove the script for brevity).

yes, you're right. it's just an illustrated input. but, i still need
`cat`. i use such a more complex pipeline (about 5) and put the
outputs to an array.
your explanation is satisfying me.
thanks a lot, Cam Hutchison.

Greetings,

Marco


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: 
http://lists.debian.org/CAD9kJ0O8K7_=o-8zNHbUK5+CcCETsB9RveTht4vA==9f0yq...@mail.gmail.com



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

2012-09-16 Thread Cam Hutchison
Morning Star  writes:

>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

When awk runs, it reads its input until EOF. In your loop, the first run
of awk is consuming all the input from stdin (cat input) and printing
the first line. For the subsequent iterations through the loop, awk no
longer has anything to read - it gets EOF when attempting to read the first
line. This means the script never matches anything and will not print
anything.

You will need to have awk re-read the input each time:

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

I've added an "exit" to the awk script since after the action is executed,
there is clearly no more work to be done for the rest of the file, so it
make sense to terminate early.

"cat input | " is not needed - it's a useless use of cat, but I don't
know if you have it here as a representation of a more complex pipeline
that is not relevant to your question. If you are literally using
"cat input | ", you can replace it with either:

  gawk -v var=$1 '...' input

(i've remove the script for brevity).


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: http://lists.debian.org/5f17.50567574.c2...@xionine.xdna.net



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

2012-09-16 Thread Morning Star
On Sun, Sep 16, 2012 at 4:28 PM, Christofer C. Bell
 wrote:

> $ 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.

Thanks, Chris. you've clear the dirty dust in my eyes. :)

--
Marco


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: 
http://lists.debian.org/CAD9kJ0OUm60jnSibN-MbPUEJWZBfBBUwxMT_=dnpzuhuk5y...@mail.gmail.com



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

2012-09-16 Thread Christofer C. Bell
On Sun, Sep 16, 2012 at 3:44 AM, Morning Star
 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


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: 
http://lists.debian.org/caoevnytu51f1ki_src8oc06lo7vhh_gzuniugux3ukzpvcb...@mail.gmail.com



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

2012-09-16 Thread Morning Star
On Sun, Sep 16, 2012 at 3:53 PM, Teemu Likonen  wrote:

> Maybe not what you are asking for but one can get the desired output
> with these:
>
> $ awk 'NR >= 1 && NR <= 3 { print }' inputfile
>
> $ head -n3 inputfile
>
> $ sed -n 1,3p inputfile
>

thanks, Teemu. i already know that, but right now i need to understand
how passing for loop bash variables to awk works.


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: 
http://lists.debian.org/CAD9kJ0Nofx3Thr=eBN4zM=JN=bjop4cksx5jp0vnmdrrxzz...@mail.gmail.com



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

2012-09-16 Thread Morning Star
On Sun, Sep 16, 2012 at 4:14 PM, emmanuel segura  wrote:
> awk '/line/ {print; if(FNR % 3 == 0){exit}}' var
>

thanks, emmanuel. i already know that, but right now i need to understand
how passing for loop bash variables to awk works.


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: 
http://lists.debian.org/CAD9kJ0P7XL+g=oN4ra-WojhpfGe8urPT0w=bewd2m1uz_62...@mail.gmail.com



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

2012-09-16 Thread Morning Star
On Sun, Sep 16, 2012 at 3:59 PM, Alex Hutton  wrote:

> I would do :
> cat input | head -n3
>

thanks, alex. i already know that, but right now i need to understand
how passing for loop bash variables to awk works.


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: 
http://lists.debian.org/cad9kj0n1qy923nyqhs+c-m2svnywzlcd3dz1oasroh157cw...@mail.gmail.com



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

2012-09-16 Thread Teemu Likonen
Morning Star [2012-09-16 15:44:05 +0700] wrote:

> 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:

> 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

Maybe not what you are asking for but one can get the desired output
with these:

$ awk 'NR >= 1 && NR <= 3 { print }' inputfile

$ head -n3 inputfile

$ sed -n 1,3p inputfile


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: http://lists.debian.org/874nmytkk9@mithlond.arda



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

2012-09-16 Thread Morning Star
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

i would be glad if somebody help me. Thanks.

Greetings,

Marco


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: 
http://lists.debian.org/CAD9kJ0M6Dd8tZ2-kxO5D=ia+hzraho+61nypn50qtkj-yvv...@mail.gmail.com