Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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..

like image 957
Aquarius Power Avatar asked Nov 11 '13 16:11

Aquarius Power


People also ask

How do you match a character in sed?

' [^ tab ]\+ ' (Here tab stands for a single tab character.) This matches a string of one or more characters, none of which is a space or a tab.

Can you use regex in sed?

Regular expressions are used by several different Unix commands, including ed, sed, awk, grep, and to a more limited extent, vi.

What is expression in sed?

5.1 Overview of regular expression in sed A regular expression is a pattern that is matched against a subject string from left to right. Most characters are ordinary: they stand for themselves in a pattern, and match the corresponding characters. Regular expressions in sed are specified between two slashes.

How do you end a sed?

(quit) Exit sed without processing any more commands or input. (quit) This command is the same as q , but will not print the contents of pattern space. Like q , it provides the ability to return an exit code to the caller.


1 Answers

Try following sed

sed -r 's/.*(X|Y|Width|Height)\s*:\s*([0-9]+)/n\1=\2/'
like image 174
jkshah Avatar answered Sep 19 '22 09:09

jkshah