我在以下格式的文件:的grep在搜索模式和替換如果模式不存在
========================================================
line1line1line1line1line1line1line1line1line1
line2-anything could be here
...anything again
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
========================================================
abc abc abc bacbk kjhhjkh
line2-anything could be here SOME_STRING
...anything again
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
我要搜索並替換塊(=====*
和++++*
之間定義)與說abc
其中SOME_STRING
不存在。例如,上述文件將如下所示:
========================================================
abc
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
========================================================
abc abc abc bacbk kjhhjkh
line2-anything could be here SOME_STRING
...anything again
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
爲塊1不具有SOME_STRING
。
我可以使用搜索模式
========================================================\_.\{-\}++++++++++++++++++++++++++++++++++++++++++++++++++++++++
得到塊,但後來我想給grep這個模式搜索的結果,如果grep的返回1(沒有找到匹配),與abc
更換塊。
可能嗎?提前致謝。
感謝您的答覆,但它是不可能的SED或VIM?我不明白awk。 – Saurabh
awk很神祕。你能不能概述一下你的方法?沒有這些,罐裝解決方案中的學習很少。 –
@IngoKarkat:如果awk很神祕,那麼sed將會變得更加神祕:P – anubhava