WC command of mac showing one less result

前端 未结 3 1488
萌比男神i
萌比男神i 2021-01-02 18:30

I have a text file which has over 60MB size. It has got entries in 5105043 lines, but when I am doing wc -l it is giving only 5105042 results which is one less than actual.

相关标签:
3条回答
  • 2021-01-02 19:03

    Last line does not contain a new line.

    One trick to get the result you want would be:

    sed -n '=' <yourfile> | wc -l
    

    This tells sed just to print the line number of each line in your file which wc then counts. There are probably better solutions, but this works.

    0 讨论(0)
  • 2021-01-02 19:12

    The last line in your file is probably missing a newline ending. IIRC, wc -l merely counts the number of newline characters in the file.

    If you try: cat -A file.txt | tail does your last line contain a trailing dollar sign ($)?

    EDIT:

    Assuming the last line in your file is lacking a newline character, you can append a newline character to correct it like this:

    printf "\n" >> file.txt
    

    The results of wc -l should now be consistent.

    0 讨论(0)
  • 2021-01-02 19:23

    60 MB seems a bit big file but for small size files. One option could be

    cat -n file.txt
    

    OR

    cat -n sample.txt | cut -f1 | tail -1
    
    0 讨论(0)
提交回复
热议问题