0
\$\begingroup\$

(using GNU sed)

I want to grab some output from xrandr and leave out everything but the resolution and offset.

Input:

eDP1 connected 1366x768+0+0 (normal left inverted right x axis y axis) 290mm x 160mm
   1366x768      60.02*+
   1280x720      59.74  
   ...
   640x360       59.84    59.32    60.00  
HDMI1 connected primary 2560x1440+1366+0 (normal left inverted right x axis y axis) 610mm x 350mm
   3840x2160     30.00 +  25.00    24.00    29.97    23.98  
   2560x1440     59.95* 
   ...
   720x400       70.08

Desired output:

1366x768+0+0
2560x1440+1366+0

This is my current command:

xrandr | sed -e '/^\ /d' -e 's/.*\(\<.*x[0-9]*+[0-9]*+[0-9]*\).*/\1/'

The first expression deletes all the lines that begin with spaces, leaving

eDP1 connected 1366x768+0+0 (normal left inverted right x axis y axis) 290mm x 160mm
HDMI1 connected primary 2560x1440+1366+0 (normal left inverted right x axis y axis) 610mm x 350mm

Then the second expression find the resolution/offset and throws away everything else.

How could I make the regex cleaner?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Cleaner is very much in the eye of the beholder.

Your regex is fine - although for the digits part - I would use \d+ because you expect at least 1 digit. And \< only works in GNU sed (not on OSX for instance).

Alternative you can look at it differently .. e.g only print the lines you care about, and on those lines drop everything you don't care about).

sed -n -e '/connected/ { s/ (.*$//; s/^.*ected //; s/^primary //; p; }'
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.