user1684072
user1684072

Reputation: 169

Longest line using awk

Can someone show, how to use awk command to identify the longest line in a text file.

Thanks

Upvotes: 4

Views: 4028

Answers (2)

William Pursell
William Pursell

Reputation: 212654

To print the longest line:

awk 'length > m { m = length; a = $0 } END { print a }' input-file

To simply identify the longest line by line number:

awk 'length > m { m = length; a = NR } END { print a }' input-file

Upvotes: 9

Jonathan Leffler
Jonathan Leffler

Reputation: 755054

awk '{ if (length($0) > longest) longest = length($0); } END { print longest }'

Upvotes: 2

Related Questions