how to match one word or another, within a single line, with sed
Asked Answered
E

1

13

For this command xwininfo -id 0x8a00004 |grep "Absolute\|Width\|Height" I have this output

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

by using a single sed command, I want it to become this:

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

My current code is this:

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

I am aware that to match words with sed, I need to use \bWordToMatch\b on the expression, but I cannot find a way to put it where ([XY]) is on my command... I am aware also that to match "one OR other" word, I need to use \|, that I cant guess either..

Evacuee answered 11/11, 2013 at 16:40 Comment(0)
H
18

Try following sed

sed -r 's/.*(X|Y|Width|Height)\s*:\s*([0-9]+)/n\1=\2/'
Hallsy answered 11/11, 2013 at 16:48 Comment(3)
worked! I had to break the [] mind barrier; also the | was restricted by () (which I thought would not work..) thx!Evacuee
In GNU sed, the parentheses and vertical bar need to be escaped like this: \( \| \).Withal
This answer uses the -r flag, equivalent to GNU --regexp-extended. No need to escape with -r, but the answer should say so.Audrieaudris

© 2022 - 2024 — McMap. All rights reserved.