Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.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
String 如何删除包含N个以上单词的行_String_Bash - Fatal编程技术网

String 如何删除包含N个以上单词的行

String 如何删除包含N个以上单词的行,string,bash,String,Bash,bash中是否有一个好的单行程序可以从文件中删除包含N个以上单词的行 输入示例: I want this, not that, but thank you it is very nice of you to offer. The very long sentence finding form ordering system always and redundantly requires an initial, albeit annoying and sometimes nonsensical u

bash中是否有一个好的单行程序可以从文件中删除包含N个以上单词的行

输入示例:

I want this, not that, but thank you it is very nice of you to offer.
The very long sentence finding form ordering system always and redundantly requires an initial, albeit annoying and sometimes nonsensical use of commas, completion of the form A-1 followed, after this has been processed by the finance department and is legal, by a positive approval that allows for the form B-1 to be completed after the affirmative response to the form A-1 is received.
示例输出:

I want this, not that, but thank you it is very nice of you to offer.
在Python中,我将编写如下代码:

if len(line.split()) < 40:
    print line
注意:此答案假设问题的第一种方法:如何打印小于给定字符数的行 将awk与长度一起使用:


这将检查该行是否包含少于40个字符。如果是这样,它会打印出来。

要仅显示包含少于40个单词的行,可以使用awk:

awk 'NF < 40' file

使用默认字段分隔符,每个单词都被视为一个字段。打印少于40个字段的行。

否。它们不相同。python的str.split返回列表的结果。op计算列表中的元素数,但您只计算字符数。@hustmphrr op提到lenline.split,所以我不知道您在说什么。但是,请注意,在原来的问题中,它只是line.split,因此可能会导致混淆。很抱歉,混淆了,您在我纠正错误之前回答了。非常快!恭维@fedorqui len“你好吗”。split<10返回True。但你已经过滤掉了out@HuStmpHrrr我并没有仔细阅读Python代码,只是想到了打印比给定字符数短的行。OP提到这解决了问题,所以我仍然想知道投票失败的原因:
awk -v maxsize=40 'length($0) < maxsize' file
$ cat a
hello
how are you
i am fine but
i would like
to do other
things
$ awk 'length($0)<10' a
hello
things
sed -rn '/^.{,39}$/p' file
awk 'NF < 40' file