bash 如何使用shell计算字符串中的单词数

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

How to count number of words from String using shell

bash

提问by Yogesh Ralebhat

I want to count number of words from a String using Shell.

我想使用 Shell 计算字符串中的单词数。

Suppose the String is:

假设字符串是:

input="Count from this String"

Here the delimiter is space ' 'and expected output is 4. There can also be trailing space characters in the input string like "Count from this String ".

这里的分隔符是空格' ',预期输出是 4。输入字符串中也可以有尾随空格字符,如"Count from this String ".

If there are trailing space in the String, it should produce the same output, that is 4. How can I do this?

如果字符串中有尾随空格,它应该产生相同的输出,即 4。我该怎么做?

回答by Tuxdude

echo "$input" | wc -w

Use wc -w to count the number of words.

使用 wc -w 计算单词数。

Or as per dogbane's suggestion, the echo can be got rid of as well:

或者根据 dogbane 的建议,也可以消除回声:

wc -w <<< "$input"

If <<< is not supported by your shell you can try this variant:

如果你的 shell 不支持 <<< 你可以试试这个变体:

wc -w << END_OF_INPUT
$input
END_OF_INPUT

回答by dogbane

You don't need an external command like wcbecause you can do it in pure bashwhich is more efficient.

您不需要像这样的外部命令,wc因为您可以以bash更有效的方式执行它。

Convert the string into an array and then count the elements in the array:

将字符串转换为数组,然后计算数组中的元素:

$ input="Count from this String   "
$ words=( $input )
$ echo ${#words[@]}
4

Alternatively, use setto set positional parameters and then count them:

或者,使用set来设置位置参数,然后对它们进行计数:

$ input="Count from this String   "
$ set -- $input
$ echo $#
4

回答by qneill

To do it in pure bash avoiding side-effects, do it in a sub-shell:

要在纯 bash 中避免副作用,请在子 shell 中进行:

$ input="Count from this string "
$ echo $(IFS=' '; set -f; set -- $input; echo $#)
4

It works with other separators as well:

它也适用于其他分隔符:

$ input="dog,cat,snake,billy goat,horse"
$ echo $(IFS=,; set -f; set -- $input; echo $#)
5
$ echo $(IFS=' '; set -f; set -- $input; echo $#)
2

Note the use of "set -f" which disables bash filename expansionin the subshell, so if the caller wants expansion it should be done beforehand (Hat Tip @mkelement0).

请注意“set -f”的使用,它禁用了子 shell 中的bash 文件名扩展,因此如果调用者想要扩展它应该事先完成(Hat Tip @mkelement0)。

回答by kenorb

Try the following one-liner:

尝试以下单行:

echo $(c() { echo $#; }; c $input)

It basically defines c()function and passes $inputas the argument, then $#returns number of elements in the argument separated by whitespace. To change the delimiter, you may change IFS(a special variable).

它基本上定义了c()函数并$input作为参数传递,然后$#返回参数中由空格分隔的元素数。要更改分隔符,您可以更改IFS(特殊变量)。

回答by Henry Barber

echo "$input" | awk '{print NF}'

回答by AAAfarmclub

I'll just chime in with a perl one-liner (avoiding 'useless use of echo'):

我将使用 perl one-liner(避免“无用的使用 echo”):

perl -lane 'print scalar(@F)' <<< $input