Longest line in a file

前端 未结 14 2108
鱼传尺愫
鱼传尺愫 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 | wc -L

    and

    1. zcat | 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"
    

提交回复
热议问题