Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
1  
If you know the geometry, 1360x768, just use rather then a variable –  bodhi.zazen Sep 3 at 18:01
1  
I realize this, and already did it this way successfully. But I was trying to use the experience to increase my awk knowledge as well. :) –  Guest123ABC Sep 3 at 18:20
    
OK, good luck. There are several excellent tutorials on awk on the internet. –  bodhi.zazen Sep 3 at 18:23

6 Answers 6

  • 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

share|improve this answer
    
@Guest123ABC I've updated my answer with a shorter version of the regular expression –  Sylvain Pineau Sep 4 at 13:17

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'
share|improve this answer

You could maybe use awk substr. In your particular case :

xwininfo -root | awk '/geometry/ {print substr($2,1,8);}'

Or if you want it to work in any case :

xwininfo -root | awk '/geometry/ {print $2;}' | cut -d'+' -f1

hope this help.

share|improve this answer
    
Oh yes! Thank you very much! The first one worked! :) The second one produces two results: a 1 and then the resolution which would not work in this case, but still a cool statement for general knowledge. –  Guest123ABC Sep 3 at 18:13
2  
you should use the answer from sylvain. it's nicer. –  Diceroll Sep 3 at 18:19
1  
I've moved the grep portion into the awk command (because I can't help myself). The problem with substr is it uses fixed placements. Resolutions like 1000x1000 will break it because they're too long. –  Oli Sep 3 at 21:30
    
@Oli wow nice move. i'll not forget about that grep replacement. Thanks for the tips. You totally right about the substr, this is why i added the second command. –  Diceroll Sep 4 at 6:08

Others have already provided answers for the complete operation, but to answer only How do I get from 1360x768+0+0 to 1360x768? then I would recomment using cut as the simplest possible solution, e.g.

$ echo 1360x768+0+0 | cut -d+ -f1
1360x768
$
share|improve this answer

I also tried command line screen capture examples a few weeks ago. As an alternative solution you can use

 xdpyinfo  | grep dimensions | awk -F ' ' ' { print $2 } ' 

for detecting screen resolution.

To capture screen with internal audio and microphone, you can use

 avconv -f   pulse -i default -f x11grab -r 15 -s $(xdpyinfo  | grep dimensions | awk -F ' ' ' { print $2 } ' ) -i :0.0+0,0 -acodec libmp3lame -vcodec libx264  $(date +"%m%d%Y_%H%M%S_$HOSTNAME")_screencast.mp4
share|improve this answer

just add | cut -f1 -d+ at the end of your command. something like this:

xwininfo -root | grep 'geometry' | awk '{print $2;}' | cut -f1 -d+
  • -f list
    Cut based on a list of fields, assumed to be separated in the file by a delimiter character (see -d). Each selected field shall be output. Output fields shall be separated by a single occurrence of the field delimiter character. Lines with no field delimiters shall be passed through intact, unless -s is specified. It shall not be an error to select fields not present in the input line.

  • -d delim
    Set the field delimiter to the character delim. The default is the <tab>.

See more command type man cut in terminal.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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