Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 使用AWK计算超过N个字的行数_Bash_Awk - Fatal编程技术网

Bash 使用AWK计算超过N个字的行数

Bash 使用AWK计算超过N个字的行数,bash,awk,Bash,Awk,我有一个示例文件sample.txt,其内容如下: this is me hello my name is x awk tutorials unable to get this right 我想计算超过N个单词的行数。让我们考虑n=3 因此,所需输出为2第3行和5行 我尝试了以下代码: cat sample.txt | awk'开始{count=0}{NF>3然后计数+++}结束{print count}' 我的输出总是5,这是文件中的行数;即使N从3变为8,输出也保持不变,这表明该条件始终

我有一个示例文件sample.txt,其内容如下:

this is me
hello
my name is x
awk tutorials
unable to get this right
我想计算超过N个单词的行数。让我们考虑n=3

因此,所需输出为2第3行和5行

我尝试了以下代码:

cat sample.txt | awk'开始{count=0}{NF>3然后计数+++}结束{print count}'


我的输出总是5,这是文件中的行数;即使N从3变为8,输出也保持不变,这表明该条件始终为真。

请尝试以下操作

 awk -v num="3" 'NF>num{count++} END{print count}'  Input_file

OP的代码尝试出现问题:

OP的条件NF>3的方法count++不正确。 尽管寻找NF>3的行,它的计数值每次递增,因此每次输出为5。
你能试试下面的吗

 awk -v num="3" 'NF>num{count++} END{print count}'  Input_file

OP的代码尝试出现问题:

OP的条件NF>3的方法count++不正确。 尽管寻找NF>3的行,它的计数值每次递增,因此每次输出为5。