Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWK script to print line with the largest number of fields

Tags:

linux

unix

awk

gawk

The script below displays the largest number of fields in twister.txt.

  awk '{if (NF > max) max = NF} END{print max}' twister.txt

My question is, How do you display the line itself, which has the largest number of fields in twister.txt.

like image 209
anansharm Avatar asked Feb 14 '23 11:02

anansharm


1 Answers

awk '{if (NF > max) {max = NF; line=$0}} END{print line}' twister.txt
like image 113
Håkon Hægland Avatar answered Feb 17 '23 00:02

Håkon Hægland