1

Using xrandr | grep '*' I would like to find the x resolution of all monitors on our network. This is to assist in the automatic placement of xterms in other scripts.

Some of our systems are running redhat 6.4 and others redhat 5.8 and the results of xrandr differs depending on redhat version

For redhat 6.4 xrandr | grep '*' returns

1680x1050 60.0*+

and for redhat 5.8

*0 1680 x 1050 ( 474mm x 303mm ) *50

I have tried xrandr | grep '*' | sed 's/\s+\(\d\{4\}\)\s*x\s*\d+/\1/' but this returns the same string as highlighted above for 5.8 and 6.4. The desired output is 1680 in both cases

Any hints?

moadeep
  • 3,988
  • 10
  • 45
  • 72

1 Answers1

1

try this line:

xrandr|grep -Po '\d+(?=\s*x.*\*.*)'

example:

kent$ echo '1680x1050 60.0*+
*0 8888 x 1050 ( 474mm x 303mm ) *50
without star'|grep -Po '\d+(?=\s*x.*\*.*)'
1680
8888

In the example above I changed one X to 8888 and add oneline without * to show that it works for those cases.

another way you could try, using xdpyinfo

 xdpyinfo| grep dimens|grep -oP '(?<=\s)\d+'
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
Kent
  • 189,393
  • 32
  • 233
  • 301
  • 2
    This `(master|✔)` I assume is part of your prompt.. add confusion *i.m.o*. – Chris Seymour Mar 19 '13 at 10:20
  • @sudo_O oh sorry, I didn't notice it. My current directory is a personal project managed by git. I remove it right now. – Kent Mar 19 '13 at 10:22
  • The solution works great @Kent thanks. Can you explain the ?= part of the regex. I can just about work out what the rest of the regex means – moadeep Mar 19 '13 at 10:27
  • 1
    @moadeep `(?=..)`is look-ahead assertion. e.g. `foo(?=bar)` means match `foo` only if it is followed by `bar`. This regex will only match the first `foo` on string `xxx foobar yyyfoozzz` – Kent Mar 19 '13 at 10:34