Longest line using awk

前端 未结 2 1524
灰色年华
灰色年华 2021-01-06 10:09

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

Thanks

相关标签:
2条回答
  • 2021-01-06 10:39

    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
    
    0 讨论(0)
  • 2021-01-06 10:58
    awk '{ if (length($0) > longest) longest = length($0); } END { print longest }'
    
    0 讨论(0)
提交回复
热议问题