Only get the H+W geometry of my screen without the later +x+y
I am reading this article. There is a statement there that goes:
ffmpeg -f alsa -ac 2 -i hw:0,0 -f x11grab -s $(xwininfo -root | grep 'geometry' | awk '{print $2;}') -r 25 -i :0.0 -sameq -f mpeg -ar 48000 -s wvga -y sample.mp4
When I run the command I get an error with the section that says:
xwininfo -root | grep 'geometry' | awk '{print $2;}'
The reason is that when you use this command on my computer it outputs:
1360x768+0+0
How do I remove the last part of the screen resolution output to be 1360x768
instead of 1360x768+0+0
?
-
You can use
perl
to get only the resolution:xwininfo -root | perl -lne 's/.*geometry (\w+).*/$1/ or next; print' 1360x768
-
Or even shorter with just GNU
grep
:xwininfo -root | grep -oP '(?<=geometry )\w+' 1360x768
Explanation: The lookbehind
(?<=geometry )
asserts that at the current position in the string, what precedes is the characters "geometry ". If the assertion succeeds, the engine matches the resolution pattern.A lookbehind does not "consume" any characters on the string. This means that after the closing parenthesis, the regex engine is left standing on the very same spot in the string from which it started looking: it hasn't moved. From that position, then engine can start matching characters again.
Source: http://www.rexegg.com/regex-disambiguation.html#lookbehind
My pure awk
approach, splitting the string into fields based on spaces and plus signs:
xwininfo -root | awk -F'[+| ]' '/geometry/ {print $4}'
A similar method to Sylvain's Perl expression but with sed
:
xwininfo -root | sed -nr 's/.*geometry ([0-9x]+).*/\1/p'