假设我有这个文件:
this is 1th line
this is 2th line
<<EOM
this is 3th line
EOM>>
this is 4th line
<<EOM
this is 5th line
EOM>>
this is 6th line
无论如何都要在bash中运行命令来获取:
this is 3th line
this is 5th line
并在没有<<EOM
的情况下获取全文?
答案 0 :(得分:4)
这应该有效:
$ sed -n '/<<EOM/,/EOM>>/p' file | grep -v EOM || cat file
我的意思是......
而且......这样更好(避免删除包含EOM的标记之间的行):
$ sed -n '/<<EOM/,/EOM>>/p' file | grep -vw '<<EOM\|EOM>>' || cat file
答案 1 :(得分:2)
grep -q "<<EOM" file && awk '/<<EOM/{flag=1;next}/EOM>>/{flag=0}flag' file || cat file
如果在<<EOM
中找到file
,则会在<<EOM
和EOM>>
之间打印awk
行。仅cat
<{1}} file