使用sed

时间:2017-06-27 11:25:02

标签: linux sed replace find

请使用Linux" sed"将不胜感激。 我在文件中有如下代码块。希望提取以" aa.bb.cc.config ="开头的块。并以"}"结束。从提取的块中,找到字符串[" bb11:11211"] [" ff5rt:11211"]并从文件中删除匹配的行。

asdf={
ddd, \
fff, \
ggg \
}
ghjk
wwww
aa.bb.cc.config={"List": [ \
"aa11:222", \
"bb11:11211", \
"cc22ss:11211", \
"dd33ss:11211", \
"ff5rt:11211", \
"gg6hj:11211", \
], "dd": true, "ff": 0, "hh": 0, "jj": 10, "kk": 0 }

vsdfsdf
ewqewqe

输出:

asdf={
ddd, \
fff, \
ggg \
}
ghjk
wwww
aa.bb.cc.config={"List": [ \
"aa11:222", \
"cc22ss:11211", \
"dd33ss:11211", \
"gg6hj:11211", \
], "dd": true, "ff": 0, "hh": 0, "jj": 10, "kk": 0 }

vsdfsdf
ewqewqe

先谢谢!!

1 个答案:

答案 0 :(得分:0)

sed 方法:

sed '/^aa\.bb\.cc\.config=/,/}$/{ /\"\(bb11\|ff5rt\):11211\"/d; }' file
  • /^aa.bb.cc.config=/,/}$/ - 地址范围,考虑从/^aa.bb.cc.config=//}$/的行

  • /\"\(bb11\|ff5rt\):11211\"/d; - 删除与捕获的模式空间中的模式匹配的行

使用扩展正则表达式-E

sed -E '/^aa\.bb\.cc\.config=/,/\}$/{ /"(bb11|ff5rt):11211"/d; }' file

输出:

asdf={
ddd, \
fff, \
ggg \
}
ghjk
wwww
aa.bb.cc.config={"List": [ \
"aa11:222", \
"cc22ss:11211", \
"dd33ss:11211", \
"gg6hj:11211", \
], "dd": true, "ff": 0, "hh": 0, "jj": 10, "kk": 0 }

vsdfsdf
ewqewqe