如何在一行内用sed匹配一个或另一个词

时间:2013-11-11 16:40:58

标签: regex sed word match

对于此命令xwininfo -id 0x8a00004 |grep "Absolute\|Width\|Height",我有此输出

Absolute upper-left X:  44
Absolute upper-left Y:  53
Width: 999
Height: 698

使用单个sed命令,我希望它成为:

nX=44
nY=53
nWidth=999
nHeight=698

我目前的代码是:

xwininfo -id 0x8a00004 |grep "Absolute\|Width\|Height" |sed -r 's".*([XY]):[[:blank:]]*(.*)"n\1=\2"'

我知道要将单词与sed匹配,我需要在表达式上使用\bWordToMatch\b,但我无法找到将([XY])置于我的命令之间的方法... 我也知道要匹配“一个或另一个”字,我需要使用\|,我猜不出......

1 个答案:

答案 0 :(得分:9)

尝试关注sed

sed -r 's/.*(X|Y|Width|Height)\s*:\s*([0-9]+)/n\1=\2/'