bash wc 作为变量的结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7119936/
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 20:55:45  来源:igfitidea点击:

results of wc as variables

bashshellwc

提问by 719016

I would like to use the lines coming from 'wc' as variables. For example:

我想使用来自 'wc' 的行作为变量。例如:

echo 'foo bar' > file.txt
echo 'blah blah blah' >> file.txt
wc file.txt

2  5 23 file.txt

I would like to have something like $lines, $wordsand $charactersassociated to the values 2, 5, and 23. How can I do that in bash?

我想要类似的东西$lines$words$characters与值25、 和相关联23。我怎么能在 bash 中做到这一点?

采纳答案by blueFast

There are other solutions but a simple one which I usually use is to put the output of wcin a temporary file, and then read from there:

还有其他解决方案,但我通常使用的一个简单解决方案是将 的输出wc放在临时文件中,然后从那里读取:

wc file.txt > xxx
read lines words characters filename < xxx 
echo "lines=$lines words=$words characters=$characters filename=$filename"
lines=2 words=5 characters=23 filename=file.txt

The advantage of this method is that you do not need to create several awkprocesses, one for each variable. The disadvantage is that you need a temporary file, which you should delete afterwards.

这种方法的优点是您不需要创建多个awk进程,每个变量一个。缺点是您需要一个临时文件,之后应将其删除。

Be careful: this does not work:

小心:这不起作用:

wc file.txt | read lines words characters filename

The problem is that piping to readcreates another process, and the variables are updated there, so they are not accessible in the calling shell.

问题是管道read创建另一个进程,并且变量在那里更新,因此它们在调用 shell 中无法访问。

Edit: adding solution by arnaud576875:

编辑:通过 arnaud576875 添加解决方案:

read lines words chars filename <<< $(wc x)

Works without writing to a file (and do not have pipe problem). It is bash specific.

无需写入文件即可工作(并且没有管道问题)。它是特定于 bash 的。

From the bash manual:

从 bash 手册:

Here Strings

   A variant of here documents, the format is:

          <<<word

   The word is expanded and supplied to the command on its standard input.

The key is the "word is expanded" bit.

关键是“字被扩展”位。

回答by Arnaud Le Blanc

In pure bash: (no awk)

在纯 bash 中:(没有 awk)

a=($(wc file.txt))
lines=${a[0]}
words=${a[1]}
chars=${a[2]}

This works by using bash's arrays. a=(1 2 3)creates an array with elements 1, 2 and 3. We can then access separate elements with the ${a[indice]}syntax.

这通过使用 bash 的数组来工作。a=(1 2 3)创建一个包含元素 1、2 和 3 的数组。然后我们可以使用${a[indice]}语法访问单独的元素。

Alternative: (based on gonvaled solution)

替代方案:(基于 gonvaled 解决方案)

read lines words chars <<< $(wc x)

Or in sh:

或者在 sh 中:

a=$(wc file.txt)
lines=$(echo $a|cut -d' ' -f1)
words=$(echo $a|cut -d' ' -f2)
chars=$(echo $a|cut -d' ' -f3)

回答by tripleee

Just to add another variant --

只是添加另一个变体-

set -- `wc file.txt`
chars=
words=
lines=

This obviously clobbers $*and related variables. Unlike some of the other solutions here, it is portable to other Bourne shells.

这显然$*与clobbers和相关变量有关。与这里的其他一些解决方案不同,它可以移植到其他 Bourne shell。

回答by simibac

I wanted to store the number of csv file in a variable. The following worked for me:

我想将 csv 文件的数量存储在一个变量中。以下对我有用:

CSV_COUNT=$(ls ./pathToSubdirectory | grep ".csv" | wc -l | xargs)
  • xargs removes the whitespace from the wc command
  • I ran this bash script not in the same folder as the csv files. Thus, the pathToSubdirectory
  • xargs 从 wc 命令中删除空格
  • 我在与 csv 文件不同的文件夹中运行了这个 bash 脚本。因此,pathToSubdirectory

回答by duedl0r

lines=`wc file.txt | awk '{print }'`
words=`wc file.txt | awk '{print }'`
...

you can also store the wcresult somewhere first.. and then parse it.. if you're picky about performance :)

您也可以wc先将结果存储在某处..然后解析它..如果您对性能很挑剔:)

回答by Konrad Rudolph

You can assign output to a variable by opening a sub shell:

您可以通过打开子 shell 将输出分配给变量:

$ x=$(wc some-file)
$ echo $x
1 6 60 some-file

Now, in order to get the separate variables, the simplest option is to use awk:

现在,为了获得单独的变量,最简单的选择是使用awk

$ x=$(wc some-file | awk '{print }')
$ echo $x
1

回答by Adrian Pronk

declare -a result
result=( $(wc < file.txt) )
lines=${result[0]}
words=${result[1]}
characters=${result[2]}
echo "Lines: $lines, Words: $words, Characters: $characters"