How to count lines in a document?

2019-01-05 06:25发布

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.00   94.00
09:16:40 AM  all    5.00    0.00    0.00    4.00    0.00    0.00    0.00    0.00   91.00
09:16:41 AM  all    0.00    0.00    4.00    0.00    0.00    0.00    0.00    0.00   96.00
09:16:42 AM  all    3.00    0.00    1.00    0.00    0.00    0.00    0.00    0.00   96.00
09:16:43 AM  all    0.00    0.00    1.00    0.00    1.00    0.00    0.00    0.00   98.00
09:16:44 AM  all    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
09:16:45 AM  all    2.00    0.00    6.00    0.00    0.00    0.00    0.00    0.00   92.00

Is there a way to count them all using linux commands?

19条回答
时光不老,我们不散
2楼-- · 2019-01-05 07:02

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

cat -n <filename>

Will show you whole content of file with line numbers.

查看更多
姐就是有狂的资本
3楼-- · 2019-01-05 07:05

Use wc:

wc -l <filename>
查看更多
看我几分像从前
4楼-- · 2019-01-05 07:05

Or count all lines in subdirectories with a file name pattern (e.g. logfiles with timestamps in the file name):

wc -l ./**/*_SuccessLog.csv
查看更多
够拽才男人
5楼-- · 2019-01-05 07:06

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
查看更多
倾城 Initia
6楼-- · 2019-01-05 07:08
wc -l <file.txt>

Or

command | wc -l
查看更多
Deceive 欺骗
7楼-- · 2019-01-05 07:10

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
查看更多
登录 后发表回答