开发者

regular expression, multiline

I want to delete 开发者_Go百科all the text between a pair of "};" which contains a particular keyword. What i want is

input:

}; text text KEYWORD text text };

Output:

};   };

Suggest me a simple regular expression. I know 'sed' is to be used.


This should work under most conditions:

sed '/};[^}]*};/{s/};[^}]*};/}; };/;b};/};/!b;:a;N;/\n[^}]*};/!ba;s/[^;]*\n.*\n[^}]*/ /' inputfile

There will probably be some corner cases where this fails. Change the space near the end to \n if you want the result to be on two lines.

Examples:

}; test ;} becomes }; };

};
test
};
becomes }; };

abc };
test
}; def
becomes abc }; }; def

abc }; 111
test1
test2
222 }; def
becomes abc }; }; def


\};[^}]*KEYWORD[^}]*\};

will work if there are no } between the two delimiters.

So:

sed 's/\};[^}]*KEYWORD[^}]*\};/}; };/g' file.in > file.out


Below regex will match the thing that you want to delete -

(?<=\};).*?KEYWORD.*?(?=\};)

Edit: this wont work with sed as pointed out by @Tim as sed does not support lookarounds.


The simplest approach possible:

cat file.in | sed "/KEYWORD/s/};[^}]*};/}; };/g" > file.out
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜