Take the 2-minute tour ×

We all know that there are several built-in variables in awk utility like - NF, FS, NR, and positional variables like $0 ... . I have an awk script where I am using getline functionality to get output of a command like below:

 while ( ("ls -l" |& getline) > 0) {
 while ( ("ls -l" | getline) > 0) {
while ( ("ls -l" |& getline var) > 0) {

Please let me know which of the AWK built-in variables I can infer inside the while loop. I tried accessing NR but it always gives me 0? Say I want to write something like below:

  if(NR == 1)
      do something .. by parsing data via a Field Separator
share|improve this question

1 Answer 1

up vote 1 down vote accepted

See http://awk.info/?tip/getline and make sure you fully understand it and have a lot of awk experience before being tempted to use getline. The example you give is almost certainly NOT a good candidate for using getline.

share|improve this answer
Thanks a lot for the link. Please let me some reasons why my example given is NOT a good candidate for using getline. I do not have much exposure on awk scripts and I am using it to extract output of application log files, some Unix system commands to generate a profile / details of my box. –  Prakash Dec 12 '12 at 13:54
That's the wrong way to think about it. You should NOT use getline by default as it's unnecessary, complicated, error-prone, unmaintainable, etc., etc. unless used in the very few, very specific cases where it's appropriate (see that article again). The question you should ask is "why IS this a good candidate for using getline". If you post a new question like "I have this input how do I produce this output" with some details on what the script has to do and what you've tried so far, we can help you write a script to do that and it almost certainly will not involve getline. –  Ed Morton Dec 12 '12 at 14:01
Earlier I used getline in this way --> awk 'BEGIN{while ( ("ls -l" | getline) > 0) {a++; if(a==4) print}}' Now I know that this is not a good way. –  Tedee12345 Dec 14 '12 at 10:41
Right, the good way is ls -l | awk 'NR==4' –  Amit Naidu Jun 2 '13 at 23:50

Your Answer


