使用sed / grep / awk删除所有内容,直到第一个空白行

时间:2012-02-13 03:35:41

标签: sed awk grep

任何人都可以帮我弄清楚如何做到这一点,我们将不胜感激。

例如

block of            //delete
non-important text  //delete

important text      //keep
more important text //keep

4 个答案:

答案 0 :(得分:10)

sed '1,/^$/d' file

awk '!$0{f=1;next}f{print}' file

输出

$ sed '1,/^$/d' <<< $'block of\nnon-important text\n\nimportant text\nmore important text'
important text
more important text

$ awk '!$0{f=1;next}f{print}' <<< $'block of\nnon-important text\n\nimportant text\nmore important text'
important text
more important text

答案 1 :(得分:5)

如果空白行为空,则执行此操作:

sed '1,/^$/d' filename

答案 2 :(得分:0)

用awk:

awk "BEGIN { x = 0 } /^$/ { x = 1} { if (x == 2) { print } ; if (x == 1) { x = 2 } }" filename

答案 3 :(得分:0)

grep的另一个选项(在线工作)

grep -v PATTERN filename > newfilename

例如:

filename包含以下行:

this is not implicated but important text
this is not important text
this is important text he says
not important text he says
not this it is more important text

过滤器:

grep -v "not imp" filename > newfilename

将使用以下3行创建newfilename:

this is not implicated but important text
this is important text he says
not this it is more important text

您必须选择一个PATTERN来唯一标识您要删除的行。如果您使用"important text"的模式,它将匹配所有行,而"not imp"仅匹配其中包含单词"not imp"的行。如果您希望在模式匹配方面有更多灵活性,请使用egrep(或grep -E)进行正则表达式过滤。