sed - 从多行输入中仅获取替换的字符串省略不匹配的行!
我希望 sed 忽略所有不匹配的行,并且仅输出替换的字符串(单个/多个预期行的)。
换句话说:我有一个干草堆,只想归还针,而不是所有被搜索且未改变的干草。
或者换句话说:在多行字符串中搜索/替换 RegEx 描述的字符串,并且仅返回该字符串。 (因为可以使用 PHP 函数 http://www.php。 net/manual/en/function.preg-replace.php )
我当前的解决方法是首先使用 grep 进行过滤,然后仅将匹配的行通过管道传输到 sed 中进行替换:
echo -e "Bla\nBla\nImportant1: One \nBla\nImportant2: Two\nBla\nBla" | egrep "^Important1.*$" | sed -E "s/^Important1: *\b(.*)\b */\1/g"
# From the multiple line input I only want the "One One" (with pre/post whitespace removed, hence matching the word boundaries with "\b")
# And I want no "Bla bla" lines in the result!
但是我'我希望在 sed 中有一个单一解决方案。或者这超出了 sed 的预期用途,因此我应该更好地使用其他东西吗?顺便说一句,问题: 使用反向引用的多行 sed 似乎有某种相关性,但我不确定!
I want sed to omit all non-matching lines, and only output the replaced string (of the single/multiple intended line/s).
In other words: I've got a hay stack, and only want the needle returned, not all the hay which was searched and which remained unaltered.
Or again in other words: Search/replace a RegEx described string in a multi line string, and only get that string returned. (As it is possible with the PHP function http://www.php.net/manual/en/function.preg-replace.php )
My current workaround is to first filter with grep, and then pipe only the matching lines into sed for replacement:
echo -e "Bla\nBla\nImportant1: One \nBla\nImportant2: Two\nBla\nBla" | egrep "^Important1.*$" | sed -E "s/^Important1: *\b(.*)\b */\1/g"
# From the multiple line input I only want the "One One" (with pre/post whitespace removed, hence matching the word boundaries with "\b")
# And I want no "Bla bla" lines in the result!
But I'd like to have a single solution within sed. Or is this out of intended sed usage, and should I therefore better use something else? Btw, issue: multiline sed using backreferences seemed somehow related, but I am not sure!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下解决方案已在 Mac 和 Mac 上进行了测试Linux。
你可以像这样使用 sed:
输出:
解释
Following solution has been tested on both Mac & Linux.
You can use sed like this:
OUTPUT:
Explanation
概念验证
Proof of Concept
此 sed 命令执行的操作与 egrep 和 sed 的组合执行的操作相同:
执行替换并在替换后仅打印匹配的行。
This sed command does what your combination of egrep and sed does:
You perform the substitution and only print lines that matched, after the substitution.
为了保留原始表达式:
您可以使用
sed
的-n
选项,并将p
标志添加到的末尾>s
命令如下:证明:
s 命令 使用以下格式:
-n
或--silent
选项禁止自动打印模式空间 1。p
标志用于在进行替换时打印新的模式空间2。In order to keep your original expression:
you can use the
-n
option forsed
and add thep
flag to the end of yours
command like this:proof:
The s command uses the following format:
The
-n
or--silent
option suppresses automatic printing of pattern space 1.The
p
flag is used to print the new pattern space if a substitution was made2.