How to count lines in a document?

后端 未结 24 1412
慢半拍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:10

    Use wc:

    wc -l <filename>
    
    0 讨论(0)
  • 2020-11-27 09:10

    Redirection/Piping the output of the file to wc -l should suffice, like the following:

    cat /etc/fstab | wc -l
    

    which then would provide the no. of lines only.

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

    As others said wc -l is the best solution, but for future reference you can use Perl:

    perl -lne 'END { print $. }'
    

    $. contains line number and END block will execute at the end of script.

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

    I saw this question while I was looking for a way to count multiple files lines, so if you want to count multiple file lines of a .txt file you can do this,

    cat *.txt | wc -l
    

    it will also run on one .txt file ;)

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

    I know this is old but still: Count filtered lines

    My file looks like:

    Number of files sent
    Company 1 file: foo.pdf OK
    Company 1 file: foo.csv OK
    Company 1 file: foo.msg OK
    Company 2 file: foo.pdf OK
    Company 2 file: foo.csv OK
    Company 2 file: foo.msg Error
    Company 3 file: foo.pdf OK
    Company 3 file: foo.csv OK
    Company 3 file: foo.msg Error
    Company 4 file: foo.pdf OK
    Company 4 file: foo.csv OK
    Company 4 file: foo.msg Error
    

    If I want to know how many files are sent OK:

    grep "OK" <filename> | wc -l
    

    OR

    grep -c "OK" filename
    
    0 讨论(0)
  • 2020-11-27 09:14

    wc -l <filename>

    This will give you number of lines and filename in output.

    Eg.

    wc -l 24-11-2019-04-33-01-url_creator.log

    Output

    63 24-11-2019-04-33-01-url_creator.log

    Use

    wc -l <filename>|cut -d\ -f 1

    to get only number of lines in output.

    Eg.

    wc -l 24-11-2019-04-33-01-url_creator.log|cut -d\ -f 1

    Output

    63

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