1
\$\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?

asked Mar 16, 2021 at 2:42
\$\endgroup\$

1 Answer 1

1
\$\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; }'
answered Mar 16, 2021 at 10:55
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.