bash echo 在没有文件名的 bash 变量中给出的文件行数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12022319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:33:31  来源:igfitidea点击:

bash echo number of lines of file given in a bash variable without the file name

bashwc

提问by Marcus Junius Brutus

I have the following three constructs in a bash script:

我在 bash 脚本中有以下三个结构:

NUMOFLINES=$(wc -l $JAVA_TAGS_FILE)
echo $NUMOFLINES" lines"

echo $(wc -l $JAVA_TAGS_FILE)" lines"

echo "$(wc -l $JAVA_TAGS_FILE) lines"

And they both produce identical output when the script is run:

当脚本运行时,它们都产生相同的输出:

121711 /home/slash/.java_base.tag lines
121711 /home/slash/.java_base.tag lines
121711 /home/slash/.java_base.tag lines

I.e. the name of the file is also echoed (which I don't want to). Why do these scriplets fail and how should I output a clean:

即文件的名称也被回显(我不想)。为什么这些脚本会失败,我应该如何输出一个干净的:

121711 lines

?

?

回答by Todd A. Jacobs

An Example Using Your Own Data

使用您自己的数据的示例

You can avoid having your filename embedded in the NUMOFLINESvariable by using redirection from JAVA_TAGS_FILE, rather than passing the filename as an argument to wc. For example:

通过使用来自JAVA_TAGS_FILE 的重定向,您可以避免将文件名嵌入到NUMOFLINES变量中,而不是将文件名作为参数传递给wc。例如:

NUMOFLINES=$(wc -l < "$JAVA_TAGS_FILE")

Explanation: Use Pipes or Redirection to Avoid Filenames in Output

说明:使用管道或重定向来避免输出中的文件名

The wcutility will not print the name of the file in its output if input is taken from a pipe or redirection operator. Consider these various examples:

如果输入来自管道或重定向操作符,则wc实用程序不会在其输出中打印文件的名称。考虑这些不同的例子:

# wc shows filename when the file is an argument
$ wc -l /etc/passwd
41 /etc/passwd

# filename is ignored when piped in on standard input
$ cat /etc/passwd | wc -l
41

# unusual redirection, but wc still ignores the filename
$ < /etc/passwd wc -l
41

# typical redirection, taking standard input from a file
$ wc -l < /etc/passwd
41

As you can see, the only time wcwill print the filename is when its passed as an argument, rather than as data on standard input. In some cases, you may want the filename to be printed, so it's useful to understand when it will be displayed.

如您所见,wc打印文件名的唯一时间是将其作为参数传递,而不是作为标准输入上的数据传递。在某些情况下,您可能希望打印文件名,因此了解何时显示文件名很有用。

回答by Ignacio Vazquez-Abrams

wccan't get the filename if you don't give it one.

wc如果您不提供文件名,则无法获取文件名。

wc -l < "$JAVA_TAGS_FILE"

回答by Javier López

You can also use awk:

您还可以使用 awk:

awk 'END {print NR,"lines"}' filename

awk 'END {print NR,"lines"}' filename

Or

或者

awk 'END {print NR}' filename

awk 'END {print NR}' filename

回答by ling

(apply on Mac, and probably other Unixes)

(适用于 Mac,也可能适用于其他 Unix)

Actually there is a problem with the wc approach: it does not count the last line if it does not terminate with the end of line symbol.

实际上 wc 方法存在一个问题:如果它不以行尾符号终止,则不计算最后一行。

Use this instead

改用这个

nbLines=$(cat -n file.txt | tail -n 1 | cut -f1 | xargs)

or even better (thanks gniourf_gniourf):

甚至更好(感谢 gniourf_gniourf):

nblines=$(grep -c '' file.txt)

Note: The awk approach by chilicuil also works.

注意:chilicuil 的 awk 方法也有效。

回答by Slava Semushin

It's a very simple:

这是一个非常简单的:

NUMOFLINES=$(cat $JAVA_TAGS_FILE | wc -l )

or

或者

NUMOFLINES=$(wc -l $JAVA_TAGS_FILE | awk '{print }')

回答by Russ Hore

I normally use the 'back tick' feature of bash

我通常使用 bash 的“反勾”功能

export NUM_LINES=`wc -l filename`

Note the 'tick' is the 'back tick' e.g. ` not the normal single quote

注意'tick'是'back tick',例如`不是普通的单引号