sed replace string with condition
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
精彩评论