Sed或Grep替换直到模式匹配后的行尾 [英] Sed or Grep to replace until end of line after pattern match
本文介绍了Sed或Grep替换直到模式匹配后的行尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在模式匹配后替换所有字符,直到行尾,同时保持匹配的模式
I need to replace all the characters after a pattern match until the end of the line while keeping the matched pattern
blhablahPATTERN1XXXXXX
应阅读
blhablahPATTERN1STUFFI_WANT_TO_INSERT
到目前为止,我尝试过的所有操作也删除了PATTERN1 ...
Everything I've tried so far has deleted the PATTERN1 as well...
推荐答案
使用sed
sed -r 's/(PATTERN1).*/\1STUFFI_WANT_TO_INSERT/' file
您需要捕获 PATTERN1 并在替换字符串中使用\1
You need to capture PATTERN1 and use \1
in replacement string
这篇关于Sed或Grep替换直到模式匹配后的行尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文