1748216 Members
3499 Online
108759 Solutions
New Discussion юеВ

EOF within awk?

 
SOLVED
Go to solution
Tim Howell
Frequent Advisor

EOF within awk?

If the following was invoked...
awk -f script.awk `ls -t /tmp/logdir/log.*`

Is there a way within script.awk to know when you have reached the end of an individual file? (or know when all files have been processed?) script.awk matches patterns and processes an indeterminant number of records in each file. I want to totalize the value of these records.
TIA
if only we knew...
7 REPLIES 7
Steven E. Protter
Exalted Contributor

Re: EOF within awk?

Shalom,

how about inbedding awk in a read loop

while not EOF
do
nice awk stuff
done < filename

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: EOF within awk?

Yes awk has BEGIN and END blocks. When the normal block is finished (or if an explicit exit is done) the END block is executed.
This example should total the sizes of the files using an ls -l:

my.awk
-------------------------
BEGIN {
tot = 0
}
{
tot += ($5 + 0)
}
END {
print "Total: ",tot
}
--------------------------------------

Invoke like this:
ls -l | awk -f my.awk

Note the ($5 + 0). That is intential as it forces the 5th field (the file length in ls -l) into a numeric context. The standard idiom to force a field to string context is to append a null string.
If it ain't broke, I can fix that.
Tim Howell
Frequent Advisor

Re: EOF within awk?

Thanks,
the BEGIN & END were what I needed - the proper format and syntax was what I was lacking...your example answered my question

if only we knew...
Tim Howell
Frequent Advisor

Re: EOF within awk?

The END block is executed when the last record in the last file is processed apparently, and that's what I needed.
if only we knew...
A. Clay Stephenson
Acclaimed Contributor

Re: EOF within awk?

Note that the BEGIN block is not strictly needed as the variables are initialized as null strings -- which would evaluate in numeric context as zero but I don't never trust nobody for nothing so I always initialize values like "tot" in this example.
If it ain't broke, I can fix that.
Mike Stroyan
Honored Contributor

Re: EOF within awk?

You can track individual files using FILENAME. It will be updated line by line. This example accumulates values in an array indexed by FILENAME.

awk '{c[FILENAME]+=$2}END {for (f in c) {print f,c[f]}}' *
James R. Ferguson
Acclaimed Contributor

Re: EOF within awk?

Hi Tim:

How about(?):

# cat .nbrlines
#!/usr/bin/perl
while (<>) {
if (eof) {
printf "%8d %s\n", $., $ARGV;
close ARGV if eof;
}
}

...now:

# ./nbrlines /etc/hosts /etc/services
21 /etc/hosts
187 /etc/services

Regards!

...JRF...