awk 'NF != 0 {++count} END {print count}' list
* Awk is useful for performing simple iterative computations for which a more sophisticated language like C might prove overkill. Consider the Fibonacci sequence:
1 1 2 3 5 8 13 21 34 ...
awk 'BEGIN {a=1;b=1; while(++x<=10){print a; t=a;a=a+b;b=t}; exit}'
This generates the following output data:
1 2 3 5 8 13 21 34 55 89
* Sometimes an Awk program needs to be used repeatedly. In that case, it's simple to execute the Awk program from a shell script. For example, consider an Awk script to print each word in a file on a separate line. This could be done with a script named "words" containing:
awk '{c=split($0, s); for(n=1; n<=c; ++n) print s[n] }' $1
The method for achieving this is also simple, and involves using a variable named "skip". This variable is set to "1" every time a blank line is skipped, to tell the Awk program not to skip the next one. The scheme is as follows:
BEGIN {set skip to 0}
scan the input:
if skip == 0 if line is blank
skip = 1
else
print the line
get next line of input
if skip == 1 print the line
skip = 0
get next line of input
This translates directly into the following Awk program:
BEGIN {skip = 0}
skip == 0 {if (NF == 0)
{skip = 1}
else
{print};
next}
skip == 1 {print;
skip = 0;
next}
My awk try :
ls | gawk '{for(i=0;i<=NF;i++) {if(i==0) {print $i}}}'
No comments:
Post a Comment