On Mon, Sep 17, 2012 at 7:57 AM, Cam Hutchison <c...@xdna.net> 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

Reply via email to