> Each day I have a file generated with several hundred lines. I'd like to
> create a single awk script that would print to screen only the line(s)
> with the most fields. I've futzed around with this and come to a dead
> end, brain-dead probably. Found lots of interesting examples that help
> not. O'Reilly's sed & awk book hasn't provided the inspiration I'd hoped
> for. A nudge in the right direction sure would be appreciated.
Are you specifically wanting to use AWK for any reason, and not another
scripting language? Are you looking for a command-line script, or an
executable script? When you say "the most fields", do you mean the most
fields for that file, or do you know how many fields "the most fields" is?
If the maximum number of fields is variable, you'll have to parse the file
twice; the first time to determine the max value, and the second time to
print the lines that have that many fields. The good news is that "several
hundred lines" shouldn't be very time consuming to parse twice.
I'm not an AWK guru, but the following script should work:
max = 0
while (getline < ARGV)
if (NF > max) max = NF
if (NF == max) print $0
Save this to a file called maxlines.awk and run:
awk -f maxlines.awk myfile
(where "myfile" is your log file)