简体   繁体   English

如果文件没有行尾字符,则 wc -l 不计算文件的最后一个

[英]wc -l is NOT counting last of the file if it does not have end of line character

I need to count all lines of an unix file.我需要计算一个unix文件的所有行。 The file has 3 lines but wc -l gives only 2 count.该文件有 3 行,但wc -l只给出了 2 行。

I understand that it is not counting last line because it does not have end of line character我知道它不计算最后一行,因为它没有行尾字符

Could any one please tell me how to count that line as well ?谁能告诉我如何计算那条线?

grep -c returns the number of matching lines. grep -c返回匹配行的数量。 Just use an empty string "" as your matching expression:只需使用空字符串""作为匹配表达式:

$ echo -n $'a\nb\nc' > 2or3.txt
$ cat 2or3.txt | wc -l
2
$ grep -c "" 2or3.txt
3

It is better to have all lines ending with EOL \\n in Unix files.最好在 Unix 文件中让所有行都以 EOL \\n结尾。 You can do:你可以做:

{ cat file; echo ''; } | wc -l

Or this awk:或者这个awk:

awk 'END{print NR}' file

This approach will give the correct line count regardless of whether the last line in the file ends with a newline or not.无论文件中的最后一行是否以换行符结尾,这种方法都会给出正确的行数。

awk will make sure that, in its output, each line it prints ends with a new line character. awk将确保在其输出中,它打印的每一行都以换行符结尾。 Thus, to be sure each line ends in a newline before sending the line to wc , use:因此,要确保每行在将行发送到wc之前以换行符结尾,请使用:

awk '1' file | wc -l

Here, we use the trivial awk program that consists solely of the number 1 .在这里,我们使用仅由数字1组成的简单awk程序。 awk interprets this cryptic statement to mean "print the line" which it does, being assured that a trailing newline is present. awk将这个神秘的语句解释为它所做的“打印行”,并确保存在尾随换行符。

Examples例子

Let us create a file with three lines, each ending with a newline, and count the lines:让我们创建一个包含三行的文件,每行以换行符结尾,并计算行数:

$ echo -n $'a\nb\nc\n' >file
$ awk '1' f | wc -l
3

The correct number is found.找到了正确的号码。

Now, let's try again with the last new line missing:现在,让我们在缺少最后一行的情况下重试:

$ echo -n $'a\nb\nc' >file
$ awk '1' f | wc -l
3

This still provides the right number.这仍然提供了正确的数字。 awk automatically corrects for a missing newline but leaves the file alone if the last newline is present. awk自动更正丢失的换行符,但如果存在最后一个换行符,则不处理文件。

Respect尊重

I respect the answer from John1024 and would like to expand upon it.我尊重John1024答案,并想对其进行扩展。

Line Count function行数功能

I find myself comparing line counts A LOT especially from the clipboard, so I have defined a bash function.我发现自己比较了很多行数,尤其是在剪贴板中,所以我定义了一个 bash 函数。 I'd like to modify it to show the filenames and when passed more than 1 file a total.我想修改它以显示文件名,并且总共传递了 1 个以上的文件。 However, it hasn't been important enough for me to do so far.然而,到目前为止,这对我来说还不够重要。

# semicolons used because this is a condensed to 1 line in my ~/.bash_profile
function wcl(){
  if [[ -z "${1:-}" ]]; then
    set -- /dev/stdin "$@";
  fi;
  for f in "$@"; do
    awk 1 "$f" | wc -l;
  done;
}

Counting lines without the function没有函数的计数行

# Line count of the file
$ cat file_with_newline    | wc -l
       3

# Line count of the file
$ cat file_without_newline | wc -l
       2

# Line count of the file unchanged by cat
$ cat file_without_newline | cat | wc -l
       2

# Line count of the file changed by awk
$ cat file_without_newline | awk 1 | wc -l
       3

# Line count of the file changed by only the first call to awk
$ cat file_without_newline | awk 1 | awk 1 | awk 1 | wc -l
       3

# Line count of the file unchanged by awk because it ends with a newline character
$ cat file_with_newline    | awk 1 | awk 1 | awk 1 | wc -l
       3

Counting characters (why you don't want to put a wrapper around wc )计数字符(为什么你不想在wc周围放置一个包装器)

# Character count of the file
$ cat file_with_newline    | wc -c
       6

# Character count of the file unchanged by awk because it ends with a newline character
$ cat file_with_newline    | awk 1 | awk 1 | awk 1 | wc -c
       6

# Character count of the file
$ cat file_without_newline | wc -c
       5

# Character count of the file changed by awk
$ cat file_without_newline | awk 1 | wc -c
       6

Counting lines with the function使用函数计算行数

# Line count function used on stdin
$ cat file_with_newline    | wcl
       3

# Line count function used on stdin
$ cat file_without_newline | wcl
       3

# Line count function used on filenames passed as arguments
$ wcl file_without_newline  file_with_newline
       3
       3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM