Longest line in a file

前端 未结 14 1977
鱼传尺愫
鱼传尺愫 2020-11-27 10:34

I\'m looking for a simple way to find the length of the longest line in a file. Ideally, it would be a simple bash shell command instead of a script.

相关标签:
14条回答
  • 2020-11-27 11:16

    I'm in a Unix environment, and work with gzipped files that are a few GBs in size. I tested the following commands using a 2 GB gzipped file with record length of 2052.

    1. zcat <gzipped file> | wc -L

    and

    1. zcat <gzipped file> | awk '{print length}' | sort -u

    The times were on avarage

    1. 117 seconds

    2. 109 seconds

    Here is my script after about 10 runs.

    START=$(date +%s) ## time of start
    
    zcat $1 |  wc -L
    
    END=$(date +%s) ## time of end
    DIFF=$(( $END - $START ))
    echo "It took $DIFF seconds"
    
    START=$(date +%s) ## time of start
    
    zcat $1 |  awk '{print length}' | sort -u
    
    END=$(date +%s) ## time of end
    DIFF=$(( $END - $START ))
    echo "It took $DIFF seconds"
    
    0 讨论(0)
  • 2020-11-27 11:18

    Just for fun, here's the Powershell version:

    cat filename.txt | sort length | select -last 1
    

    And to just get the length:

    (cat filename.txt | sort length | select -last 1).Length
    
    0 讨论(0)
  • 2020-11-27 11:24
    wc -L < filename
    

    gives

    101
    
    0 讨论(0)
  • 2020-11-27 11:24
    perl -ne 'print length()."  line $.  $_"' myfile | sort -nr | head -n 1
    

    Prints the length, line number, and contents of the longest line

    perl -ne 'print length()."  line $.  $_"' myfile | sort -n
    

    Prints a sorted list of all lines, with line numbers and lengths

    . is the concatenation operator - it is used here after length()
    $. is the current line number
    $_ is the current line

    0 讨论(0)
  • 2020-11-27 11:24

    Looks all the answer do not give the line number of the longest line. Following command can give the line number and roughly length:

    $ cat -n test.txt | awk '{print "longest_line_number: " $1 " length_with_line_number: " length}' | sort -k4 -nr | head -3
    longest_line_number: 3 length_with_line_number: 13
    longest_line_number: 4 length_with_line_number: 12
    longest_line_number: 2 length_with_line_number: 11
    
    0 讨论(0)
  • 2020-11-27 11:27

    Here are references of the anwser

    cat filename | awk '{print length, $0}'|sort -nr|head -1
    

    http://wtanaka.com/node/7719

    0 讨论(0)
提交回复
热议问题