开发者

sed replace string with condition

开发者 https://www.devze.com 2023-03-19 19:34 出处:网络
I have one file contains several lines, each line has the format like 2011-07-10 condition hhh aaa: value bbb

I have one file contains several lines, each line has the format like

2011-07-10 condition hhh aaa: value bbb
2011-07-10 condition ccc aaa: value bbb
开发者_JAVA百科

I want to use sed to find the value string, which is between "aaa:" and "bbb", and replace to "gotit" based on the condition that the string following condition is ccc. and after sed, this file becomes

  2011-07-10 condition hhh aaa: value bbb
  2011-07-10 condition ccc aaa: gotit bbb


Given my limited knowledge of sed/regex:

sed 's/\(condition ccc.*aaa: \).*\( bbb$\)/\1gotit\2/' file


Heres more logical:

sed '/ccc/{ s/\(.*aaa: \).*\( bbb\)/\1gotit\2/g }' inputfile

sed operates only on the line that contains ccc

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号