sed 用捕获组替换行 [英] sed replace line with capture groups
本文介绍了sed 用捕获组替换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
sed 是否可以用正则表达式中的捕获组替换一行?
Is it possible with sed to replace a line with capture groups from the regex?
我有这个正则表达式,请注意这是固定的,我无法更改.
I have this regex, please note that this is fixed, I cannot change it.
简单示例(.*)
这就是我想要的:
This is just a simple sample line with some text
更改为:
line with some text
我需要这样的东西:
sed '/simple sample \(.*\)/c \1'
现在输出:
1
有人知道您是否可以在 sed 中使用带有更改行功能的捕获组吗?
Anybody knows if you can use capture groups in sed with the change line function?
推荐答案
喜欢这个吗?
echo "This is just a simple sample line with some text" | \
sed 's/simple sample \(.*\)/\n\1/;s/.*\n//'
想法很简单:用换行符前面的捕获组替换整个正则表达式匹配.然后将包括第一个换行符在内的所有内容替换为空.当然,您可以使用换行符以外的标记.
The idea is simple: replace the whole regexp match with the captured group preceded by a newline. Then replace everything up to and including the first newline with nothing. Of course, you could use a marker other than the newline.
这篇关于sed 用捕获组替换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文