简体   繁体   English

sed 无法仅打印匹配的正则表达式 grop

[英]sed not able to print matching regex grop only

I have some key value pair arguments.我有一些键值对 arguments。 I need to print them as is.我需要按原样打印它们。 Example.例子。

echo $X
(a=b) (c=d) (e=f)
echo "$X" | sed -E 's/([a-zA-Z0-9_]*=[a-zA-Z0-9_]*)/match/1'
echo "$X" | sed -E 's/([a-zA-Z0-9_]*=[a-zA-Z0-9_]*)/\1/1'
echo "$X" | sed -E 's/([a-zA-Z0-9_]*=[a-zA-Z0-9_]*)/\1/2'
echo "$X" | sed -E 's/([a-zA-Z0-9_]*=[a-zA-Z0-9_]*)/\1/3'

Post the above expresion, I wanted to print matching groups one by one.发布上述表达式,我想一一打印匹配组。 using.* in pattern matching is greedy and is printing either first or last matching groups only.在模式匹配中使用。* 是贪婪的,并且只打印第一个或最后一个匹配组。 How can I print any matching group in this way.如何以这种方式打印任何匹配的组。

Here is my expected output.这是我预期的 output。

a=b
c=d
e=f

This grep one-liner will do:这个 grep 单线将做:

grep -o '[^(]*=[^)]*'

example:例子:

kent$  grep -o '[^(]*=[^)]*' <<<'(a=b) (c=d) (e=f)' 
a=b
c=d
e=f

Replace ) ( with a newline and remove the remaining parentheses.) (替换为换行符并删除剩余的括号。

echo "$X" | sed 's/) (/\n/g;s/[()]//g'

To print the $n th line, you can pipe the output to要打印第$n行,您可以 pipe output 到

sed -n "$n p"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM