Count lines in large files

前端 未结 13 2197
挽巷
挽巷 2020-12-02 08:53

I commonly work with text files of ~20 Gb size and I find myself counting the number of lines in a given file very often.

The way I do it now it\'s just cat fn

13条回答
  •  有刺的猬
    2020-12-02 09:38

    If your computer has python, you can try this from the shell:

    python -c "print len(open('test.txt').read().split('\n'))"
    

    This uses python -c to pass in a command, which is basically reading the file, and splitting by the "newline", to get the count of newlines, or the overall length of the file.

    @BlueMoon's:

    bash-3.2$ sed -n '$=' test.txt
    519
    

    Using the above:

    bash-3.2$ python -c "print len(open('test.txt').read().split('\n'))"
    519
    

提交回复
热议问题