删除包含多个单词的行

时间:2011-05-29 07:24:29

标签: linux bash scripting

我需要删除指定文件中的一行,如果它在linux中使用bash脚本中有多个单词。

e.g。文件:

$ cat testfile

This is a text
file

This line should be deleted
this-should-not.

7 个答案:

答案 0 :(得分:2)

awk 'NF<=1{print}' testfile

一个单词是一个非空白的运行。

答案 1 :(得分:1)

awk '!/[ \t]/{print $1}' testfile

这将显示“打印不包含空格或制表符的行的第一个元素”。 将输出空行(因为它们不包含多个单词)。

答案 2 :(得分:1)

足够简单:

$ egrep -v '\S\s+\S' testfile

答案 3 :(得分:1)

只是为了好玩,这里是一个纯粹的bash版本,它不会调用任何其他可执行文件(因为你在bash中要求它):

$ while read a b; do if [ -z "$b" ]; then echo $a;fi;done <testfile

答案 4 :(得分:0)

$ sed '/ /d' << EOF
> This is a text
> file
> 
> This line should be deleted
> this-should-not.
> EOF
file

this-should-not.

答案 5 :(得分:0)

这应该满足您的需求:

cat filename | sed -n '/^\S*$/p'

答案 6 :(得分:0)

如果您想就地编辑文件(没有任何备份),您也可以使用man ed

cat <<-'EOF' | ed -s testfile
H
,g/^[[:space:]]*/s///
,g/[[:space:]]*$/s///
,g/[[:space:]]/.d
wq
EOF
相关问题