如何计算文档中的行数?

时间:2022-02-13 00:30:09

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?

是否有一种方法可以使用linux命令对它们进行计数?

18 个解决方案

#1


1360  

Use wc:

使用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 < <文件名> :

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

You can also pipe data to wc as well:

您也可以将数据传输到wc:

$ cat /dir/file.txt | wc -l
3272485
$ curl yahoo.com --silent | wc -l
63

#2


110  

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:

或使用-v进行反匹配:

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

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

查看grep man页面查看-e、-i和-x args…

#3


60  

wc -l <file.txt>

Or

command | wc -l

#4


39  

there are many ways. using wc is one.

有很多方法。使用wc就是其中之一。

wc -l file

wc - l文件

others include

其他包括

awk 'END{print NR}' file

awk文件“{打印NR }终结”

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

sed -n '$='文件(GNU sed)

grep -c ".*" file

#5


23  

The tool wc is the "word counter" in UNIX and UNIX-like operating systems, you can also use it to count lines in a file, by adding the -l option, so wc -l foo will count the number of lines in foo. You can also pipe output from a program like this: ls -l | wc -l, which will tell you how many files are in the current directory.

工具wc是UNIX和类UNIX操作系统中的“word计数器”,通过添加-l选项,您还可以使用它来计算文件中的行数,因此wc -l foo将计算foo中的行数。您还可以从这样的程序输出:ls -l | wc -l,它将告诉您当前目录中有多少文件。

#6


20  

Use wc:

使用wc:

wc -l <filename>

#7


15  

If you want to check the total line of all the files in a directory ,you can use find and wc:

如果要检查目录中所有文件的总行,可以使用find和wc:

find . -type f -exec wc -l {} +

#8


12  

If all you want is the number of lines (and not the number of lines and the stupid file name coming back):

如果您想要的只是行数(而不是行数和返回的愚蠢文件名):

wc -l < /filepath/filename.ext

As previously mentioned these also work (but are inferior for other reasons):

正如前面提到的,这些方法也有效(但由于其他原因,它们的效果不佳):

awk 'END{print NR}' file       # not on all unixes
sed -n '$=' file               # (GNU sed) also not on all unixes
grep -c ".*" file              # overkill and probably also slower

#9


7  

Use nl like this:

使用本地语言是这样的:

nl filename

From man nl:

从人问:

Write each FILE to standard output, with line numbers added. With no FILE, or when FILE is -, read standard input.

将每个文件写入标准输出,并添加行号。没有文件,或当文件是-,阅读标准输入。

#10


5  

I've been using this:

我一直用这个:

cat myfile.txt | wc -l

I prefer it over the accepted answer because it does not print the filename, and you don't have to use awk to fix that. Accepted answer:

我更喜欢它而不是被接受的答案,因为它不打印文件名,而且您不必使用awk来修复它。答:接受

wc -l myfile.txt

wc - l myfile.txt

But I think the best one is GGB667's answer:

但我认为最好的答案是GGB667的答案:

wc -l < myfile.txt

wc - l < myfile.txt

I will probably be using that from now on. It's slightly shorter than my way. I am putting up my old way of doing it in case anyone prefers it. The output is the same with those two methods.

从现在开始我可能会用到它。它比我的路短一点。我正在用我的老方法做这件事,以防有人喜欢。输出与这两个方法相同。

#11


4  

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

以上是首选的方法,但“cat”命令也可以帮助:

cat -n <filename>

Will show you whole content of file with line numbers.

将显示带有行号的文件的整个内容。

#12


4  

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,

我看到这个问题的时候我正在寻找一种方法来计算多个文件行,所以如果你想计算。txt文件的多个文件行你可以这样做,

cat *.txt | wc -l

it will also run on one .txt file ;)

它还将在一个.txt文件上运行;

#13


3  

wc -l file.txt | cut -f3 -d" "

Returns only the number of lines

只返回行数

#14


3  

cat file.log | wc -l | grep -oE '\d+'
  • grep -oE '\d+': In order to return the digit numbers ONLY.
  • grep -oE '\d+':为了只返回数字。

#15


2  

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

将文件的输出重定向到wc -l就足够了,如下所示:

cat /etc/fstab | wc -l

which then would provide the no. of lines only.

然后给出no。行。

#16


1  

I just made a program to do this ( with node )

我刚做了一个程序来做这个(用node)

npm install gimme-lines
gimme-lines verbose --exclude=node_modules,public,vendor --exclude_extensions=html

https://github.com/danschumann/gimme-lines/tree/master

https://github.com/danschumann/gimme-lines/tree/master

#17


1  

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

#18


0  

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

正如其他人所说的,wc -l是最好的解决方案,但是对于将来的引用,您可以使用Perl:

perl -lne 'END { print $. }'

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

美元。包含行号和结束块将在脚本末尾执行。

#1


1360  

Use wc:

使用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 < <文件名> :

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

You can also pipe data to wc as well:

您也可以将数据传输到wc:

$ cat /dir/file.txt | wc -l
3272485
$ curl yahoo.com --silent | wc -l
63

#2


110  

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:

或使用-v进行反匹配:

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

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

查看grep man页面查看-e、-i和-x args…

#3


60  

wc -l <file.txt>

Or

command | wc -l

#4


39  

there are many ways. using wc is one.

有很多方法。使用wc就是其中之一。

wc -l file

wc - l文件

others include

其他包括

awk 'END{print NR}' file

awk文件“{打印NR }终结”

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

sed -n '$='文件(GNU sed)

grep -c ".*" file

#5


23  

The tool wc is the "word counter" in UNIX and UNIX-like operating systems, you can also use it to count lines in a file, by adding the -l option, so wc -l foo will count the number of lines in foo. You can also pipe output from a program like this: ls -l | wc -l, which will tell you how many files are in the current directory.

工具wc是UNIX和类UNIX操作系统中的“word计数器”,通过添加-l选项,您还可以使用它来计算文件中的行数,因此wc -l foo将计算foo中的行数。您还可以从这样的程序输出:ls -l | wc -l,它将告诉您当前目录中有多少文件。

#6


20  

Use wc:

使用wc:

wc -l <filename>

#7


15  

If you want to check the total line of all the files in a directory ,you can use find and wc:

如果要检查目录中所有文件的总行,可以使用find和wc:

find . -type f -exec wc -l {} +

#8


12  

If all you want is the number of lines (and not the number of lines and the stupid file name coming back):

如果您想要的只是行数(而不是行数和返回的愚蠢文件名):

wc -l < /filepath/filename.ext

As previously mentioned these also work (but are inferior for other reasons):

正如前面提到的,这些方法也有效(但由于其他原因,它们的效果不佳):

awk 'END{print NR}' file       # not on all unixes
sed -n '$=' file               # (GNU sed) also not on all unixes
grep -c ".*" file              # overkill and probably also slower

#9


7  

Use nl like this:

使用本地语言是这样的:

nl filename

From man nl:

从人问:

Write each FILE to standard output, with line numbers added. With no FILE, or when FILE is -, read standard input.

将每个文件写入标准输出,并添加行号。没有文件,或当文件是-,阅读标准输入。

#10


5  

I've been using this:

我一直用这个:

cat myfile.txt | wc -l

I prefer it over the accepted answer because it does not print the filename, and you don't have to use awk to fix that. Accepted answer:

我更喜欢它而不是被接受的答案,因为它不打印文件名,而且您不必使用awk来修复它。答:接受

wc -l myfile.txt

wc - l myfile.txt

But I think the best one is GGB667's answer:

但我认为最好的答案是GGB667的答案:

wc -l < myfile.txt

wc - l < myfile.txt

I will probably be using that from now on. It's slightly shorter than my way. I am putting up my old way of doing it in case anyone prefers it. The output is the same with those two methods.

从现在开始我可能会用到它。它比我的路短一点。我正在用我的老方法做这件事,以防有人喜欢。输出与这两个方法相同。

#11


4  

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

以上是首选的方法,但“cat”命令也可以帮助:

cat -n <filename>

Will show you whole content of file with line numbers.

将显示带有行号的文件的整个内容。

#12


4  

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,

我看到这个问题的时候我正在寻找一种方法来计算多个文件行,所以如果你想计算。txt文件的多个文件行你可以这样做,

cat *.txt | wc -l

it will also run on one .txt file ;)

它还将在一个.txt文件上运行;

#13


3  

wc -l file.txt | cut -f3 -d" "

Returns only the number of lines

只返回行数

#14


3  

cat file.log | wc -l | grep -oE '\d+'
  • grep -oE '\d+': In order to return the digit numbers ONLY.
  • grep -oE '\d+':为了只返回数字。

#15


2  

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

将文件的输出重定向到wc -l就足够了,如下所示:

cat /etc/fstab | wc -l

which then would provide the no. of lines only.

然后给出no。行。

#16


1  

I just made a program to do this ( with node )

我刚做了一个程序来做这个(用node)

npm install gimme-lines
gimme-lines verbose --exclude=node_modules,public,vendor --exclude_extensions=html

https://github.com/danschumann/gimme-lines/tree/master

https://github.com/danschumann/gimme-lines/tree/master

#17


1  

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

#18


0  

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

正如其他人所说的,wc -l是最好的解决方案,但是对于将来的引用,您可以使用Perl:

perl -lne 'END { print $. }'

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

美元。包含行号和结束块将在脚本末尾执行。