How to count lines in a document?

后端 未结 24 1414
慢半拍i
慢半拍i 2020-11-27 08:47

I have lines like these, and I want to know how many lines I actually have...

09:16:39 AM  all    2.00    0.00    4.00    0.00    0.00    0.00    0.00    0.0         


        
相关标签:
24条回答
  • 2020-11-27 09:25

    count number of lines and store result in variable use this command:

    count=$(wc -l < file.txt) echo "Number of lines: $count"

    0 讨论(0)
  • 2020-11-27 09:26

    To count all lines use:

    $ wc -l file
    

    To filter and count only lines with pattern use:

    $ grep -w "pattern" -c file  
    

    Or use -v to invert match:

    $ grep -w "pattern" -c -v file 
    

    See the grep man page to take a look at the -e,-i and -x args...

    0 讨论(0)
  • 2020-11-27 09:27
    wc -l file.txt | cut -f3 -d" "
    

    Returns only the number of lines

    0 讨论(0)
  • 2020-11-27 09:29

    there are many ways. using wc is one.

    wc -l file

    others include

    awk 'END{print NR}' file

    sed -n '$=' file (GNU sed)

    grep -c ".*" file
    
    0 讨论(0)
  • 2020-11-27 09:30

    Above are the preferred method but "cat" command can also helpful:

    cat -n <filename>
    

    Will show you whole content of file with line numbers.

    0 讨论(0)
  • 2020-11-27 09:32

    Use wc:

    wc -l <filename>
    

    This will output the number of lines in <filename>:

    $ wc -l /dir/file.txt
    3272485 /dir/file.txt
    

    Or, to omit the <filename> from the result use wc -l < <filename>:

    $ wc -l < /dir/file.txt
    3272485
    

    You can also pipe data to wc as well:

    $ cat /dir/file.txt | wc -l
    3272485
    $ curl yahoo.com --silent | wc -l
    63
    
    0 讨论(0)
提交回复
热议问题