Parse xrandr output to get available resolution

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
1
down vote

favorite












I have xrandr output as follows



HDMI1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 480mm x 270mm
1920x1080 60.00*+ 50.00 59.94
1920x1080i 60.00 50.00 59.94
1680x1050 59.88
1400x1050 59.95
1600x900 60.00
1280x1024 75.02 60.02
1440x900 59.90
1280x800 59.91
1152x864 75.00
1280x720 60.00 50.00 59.94
1024x768 75.03 60.00
800x600 75.00 60.32
720x576 50.00
720x480 60.00 59.94
640x480 75.00 60.00 59.94
720x400 70.08
HDMI2 disconnected (normal left inverted right x axis y axis)
VGA1 connected 1600x900+1920+0 (normal left inverted right x axis y axis) 440mm x 250mm
1600x900 60.00*+
1440x900 59.89
1280x800 59.81
1152x864 75.00
1280x720 60.00
1024x768 75.03 60.00
832x624 74.55
800x600 75.00 60.32 56.25
640x480 75.00 59.94
720x400 70.08


How can compose a bash script to get the available resolutions from xrandr result above.



for example
the-script HDMI1 will output only



HDMI1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 480mm x 270mm
1920x1080 60.00*+ 50.00 59.94
1920x1080i 60.00 50.00 59.94
1680x1050 59.88
1400x1050 59.95
1600x900 60.00
1280x1024 75.02 60.02
1440x900 59.90
1280x800 59.91
1152x864 75.00
1280x720 60.00 50.00 59.94
1024x768 75.03 60.00
800x600 75.00 60.32
720x576 50.00
720x480 60.00 59.94
640x480 75.00 60.00 59.94
720x400 70.08


I have tried



xrandr | awk '/HDMI1.*?/,/.*connected [0-9].*/'


while the result is close to what I want, but using



xrandr | awk '/VGA1.*?/,/.*connected [0-9].*/'


does not list the resolutions under VGA1 output.



Any help appreciated, thank you.







share|improve this question


























    up vote
    1
    down vote

    favorite












    I have xrandr output as follows



    HDMI1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 480mm x 270mm
    1920x1080 60.00*+ 50.00 59.94
    1920x1080i 60.00 50.00 59.94
    1680x1050 59.88
    1400x1050 59.95
    1600x900 60.00
    1280x1024 75.02 60.02
    1440x900 59.90
    1280x800 59.91
    1152x864 75.00
    1280x720 60.00 50.00 59.94
    1024x768 75.03 60.00
    800x600 75.00 60.32
    720x576 50.00
    720x480 60.00 59.94
    640x480 75.00 60.00 59.94
    720x400 70.08
    HDMI2 disconnected (normal left inverted right x axis y axis)
    VGA1 connected 1600x900+1920+0 (normal left inverted right x axis y axis) 440mm x 250mm
    1600x900 60.00*+
    1440x900 59.89
    1280x800 59.81
    1152x864 75.00
    1280x720 60.00
    1024x768 75.03 60.00
    832x624 74.55
    800x600 75.00 60.32 56.25
    640x480 75.00 59.94
    720x400 70.08


    How can compose a bash script to get the available resolutions from xrandr result above.



    for example
    the-script HDMI1 will output only



    HDMI1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 480mm x 270mm
    1920x1080 60.00*+ 50.00 59.94
    1920x1080i 60.00 50.00 59.94
    1680x1050 59.88
    1400x1050 59.95
    1600x900 60.00
    1280x1024 75.02 60.02
    1440x900 59.90
    1280x800 59.91
    1152x864 75.00
    1280x720 60.00 50.00 59.94
    1024x768 75.03 60.00
    800x600 75.00 60.32
    720x576 50.00
    720x480 60.00 59.94
    640x480 75.00 60.00 59.94
    720x400 70.08


    I have tried



    xrandr | awk '/HDMI1.*?/,/.*connected [0-9].*/'


    while the result is close to what I want, but using



    xrandr | awk '/VGA1.*?/,/.*connected [0-9].*/'


    does not list the resolutions under VGA1 output.



    Any help appreciated, thank you.







    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have xrandr output as follows



      HDMI1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 480mm x 270mm
      1920x1080 60.00*+ 50.00 59.94
      1920x1080i 60.00 50.00 59.94
      1680x1050 59.88
      1400x1050 59.95
      1600x900 60.00
      1280x1024 75.02 60.02
      1440x900 59.90
      1280x800 59.91
      1152x864 75.00
      1280x720 60.00 50.00 59.94
      1024x768 75.03 60.00
      800x600 75.00 60.32
      720x576 50.00
      720x480 60.00 59.94
      640x480 75.00 60.00 59.94
      720x400 70.08
      HDMI2 disconnected (normal left inverted right x axis y axis)
      VGA1 connected 1600x900+1920+0 (normal left inverted right x axis y axis) 440mm x 250mm
      1600x900 60.00*+
      1440x900 59.89
      1280x800 59.81
      1152x864 75.00
      1280x720 60.00
      1024x768 75.03 60.00
      832x624 74.55
      800x600 75.00 60.32 56.25
      640x480 75.00 59.94
      720x400 70.08


      How can compose a bash script to get the available resolutions from xrandr result above.



      for example
      the-script HDMI1 will output only



      HDMI1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 480mm x 270mm
      1920x1080 60.00*+ 50.00 59.94
      1920x1080i 60.00 50.00 59.94
      1680x1050 59.88
      1400x1050 59.95
      1600x900 60.00
      1280x1024 75.02 60.02
      1440x900 59.90
      1280x800 59.91
      1152x864 75.00
      1280x720 60.00 50.00 59.94
      1024x768 75.03 60.00
      800x600 75.00 60.32
      720x576 50.00
      720x480 60.00 59.94
      640x480 75.00 60.00 59.94
      720x400 70.08


      I have tried



      xrandr | awk '/HDMI1.*?/,/.*connected [0-9].*/'


      while the result is close to what I want, but using



      xrandr | awk '/VGA1.*?/,/.*connected [0-9].*/'


      does not list the resolutions under VGA1 output.



      Any help appreciated, thank you.







      share|improve this question














      I have xrandr output as follows



      HDMI1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 480mm x 270mm
      1920x1080 60.00*+ 50.00 59.94
      1920x1080i 60.00 50.00 59.94
      1680x1050 59.88
      1400x1050 59.95
      1600x900 60.00
      1280x1024 75.02 60.02
      1440x900 59.90
      1280x800 59.91
      1152x864 75.00
      1280x720 60.00 50.00 59.94
      1024x768 75.03 60.00
      800x600 75.00 60.32
      720x576 50.00
      720x480 60.00 59.94
      640x480 75.00 60.00 59.94
      720x400 70.08
      HDMI2 disconnected (normal left inverted right x axis y axis)
      VGA1 connected 1600x900+1920+0 (normal left inverted right x axis y axis) 440mm x 250mm
      1600x900 60.00*+
      1440x900 59.89
      1280x800 59.81
      1152x864 75.00
      1280x720 60.00
      1024x768 75.03 60.00
      832x624 74.55
      800x600 75.00 60.32 56.25
      640x480 75.00 59.94
      720x400 70.08


      How can compose a bash script to get the available resolutions from xrandr result above.



      for example
      the-script HDMI1 will output only



      HDMI1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 480mm x 270mm
      1920x1080 60.00*+ 50.00 59.94
      1920x1080i 60.00 50.00 59.94
      1680x1050 59.88
      1400x1050 59.95
      1600x900 60.00
      1280x1024 75.02 60.02
      1440x900 59.90
      1280x800 59.91
      1152x864 75.00
      1280x720 60.00 50.00 59.94
      1024x768 75.03 60.00
      800x600 75.00 60.32
      720x576 50.00
      720x480 60.00 59.94
      640x480 75.00 60.00 59.94
      720x400 70.08


      I have tried



      xrandr | awk '/HDMI1.*?/,/.*connected [0-9].*/'


      while the result is close to what I want, but using



      xrandr | awk '/VGA1.*?/,/.*connected [0-9].*/'


      does not list the resolutions under VGA1 output.



      Any help appreciated, thank you.









      share|improve this question













      share|improve this question




      share|improve this question








      edited May 15 at 14:03









      muru

      129k19271460




      129k19271460










      asked May 15 at 13:01









      Lee

      21029




      21029




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          A simpler version of the awk command for the script:



          xrandr |
          awk -v monitor="^$MONITOR connected" '/connected/ p = 0
          $0 ~ monitor p = 1
          p'


          Bottom to top:




          • p tells awk to run the default action (printing) depending on whether it's true or false)

          • if the line matches the monitor and is connected we set p to true

          • for all other monitor lines we set p to false





          share|improve this answer



























            up vote
            2
            down vote













            Well, figure it out,



            #!/bin/bash
            #usage the-script XRANDR_OUTPUT_NAME
            #e.g the-script HDMI1
            MONITOR=$1;
            xrandr | grep -v disconnected |
            awk '
            if(/^'$MONITOR' connected/)
            print $0;
            m="t";
            else if(m == "t")
            if (/^[a-zA-Z]/)
            exit
            else
            print $0


            '


            Hope this will help some one.






            share|improve this answer




















            • Simpler: xrandr | awk -v monitor="^$MONITOR connected" '/disconnected/ p = 0 $0 ~ monitor p = 1 p'
              – muru
              May 15 at 14:02










            • yeah, very much simpler, can you repost it as answer, to be selected as solution. thanks.
              – Lee
              May 16 at 3:57

















            up vote
            1
            down vote













            With GNU awk:



            #!/bin/sh

            xrandr --query | gawk -v monitor="$1" '
            $0 ~ monitor && $0 !~ /disconnected/
            do print
            while (getline > -1 && $0 ~ /^[[:blank:]]/)

            '


            Usage:



            $ ./the_script LVDS-1
            LVDS-1 connected primary 1440x900+0+0 (normal left inverted right x axis y axis) 303mm x 190mm
            1440x900 60.00*+ 59.89 50.00
            1360x768 59.80 59.96
            1152x864 60.00
            1024x768 60.04 60.00
            960x720 60.00
            928x696 60.05
            896x672 60.01
            960x600 60.00
            960x540 59.99
            800x600 60.00 60.32 56.25
            840x525 60.01 59.88
            800x512 60.17
            700x525 59.98
            640x512 60.02
            720x450 59.89
            640x480 60.00 59.94
            680x384 59.80 59.96
            576x432 60.06
            512x384 60.00
            400x300 60.32 56.34
            320x240 60.05



            If you don't have GNU awk, you may need to replace [[:blank:]] with [ t]






            share|improve this answer




















            • yes, another solution. thanks.
              – Lee
              May 16 at 3:58










            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "89"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1036520%2fparse-xrandr-output-to-get-available-resolution%23new-answer', 'question_page');

            );

            Post as a guest






























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            A simpler version of the awk command for the script:



            xrandr |
            awk -v monitor="^$MONITOR connected" '/connected/ p = 0
            $0 ~ monitor p = 1
            p'


            Bottom to top:




            • p tells awk to run the default action (printing) depending on whether it's true or false)

            • if the line matches the monitor and is connected we set p to true

            • for all other monitor lines we set p to false





            share|improve this answer
























              up vote
              0
              down vote



              accepted










              A simpler version of the awk command for the script:



              xrandr |
              awk -v monitor="^$MONITOR connected" '/connected/ p = 0
              $0 ~ monitor p = 1
              p'


              Bottom to top:




              • p tells awk to run the default action (printing) depending on whether it's true or false)

              • if the line matches the monitor and is connected we set p to true

              • for all other monitor lines we set p to false





              share|improve this answer






















                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                A simpler version of the awk command for the script:



                xrandr |
                awk -v monitor="^$MONITOR connected" '/connected/ p = 0
                $0 ~ monitor p = 1
                p'


                Bottom to top:




                • p tells awk to run the default action (printing) depending on whether it's true or false)

                • if the line matches the monitor and is connected we set p to true

                • for all other monitor lines we set p to false





                share|improve this answer












                A simpler version of the awk command for the script:



                xrandr |
                awk -v monitor="^$MONITOR connected" '/connected/ p = 0
                $0 ~ monitor p = 1
                p'


                Bottom to top:




                • p tells awk to run the default action (printing) depending on whether it's true or false)

                • if the line matches the monitor and is connected we set p to true

                • for all other monitor lines we set p to false






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered May 16 at 6:49









                muru

                129k19271460




                129k19271460






















                    up vote
                    2
                    down vote













                    Well, figure it out,



                    #!/bin/bash
                    #usage the-script XRANDR_OUTPUT_NAME
                    #e.g the-script HDMI1
                    MONITOR=$1;
                    xrandr | grep -v disconnected |
                    awk '
                    if(/^'$MONITOR' connected/)
                    print $0;
                    m="t";
                    else if(m == "t")
                    if (/^[a-zA-Z]/)
                    exit
                    else
                    print $0


                    '


                    Hope this will help some one.






                    share|improve this answer




















                    • Simpler: xrandr | awk -v monitor="^$MONITOR connected" '/disconnected/ p = 0 $0 ~ monitor p = 1 p'
                      – muru
                      May 15 at 14:02










                    • yeah, very much simpler, can you repost it as answer, to be selected as solution. thanks.
                      – Lee
                      May 16 at 3:57














                    up vote
                    2
                    down vote













                    Well, figure it out,



                    #!/bin/bash
                    #usage the-script XRANDR_OUTPUT_NAME
                    #e.g the-script HDMI1
                    MONITOR=$1;
                    xrandr | grep -v disconnected |
                    awk '
                    if(/^'$MONITOR' connected/)
                    print $0;
                    m="t";
                    else if(m == "t")
                    if (/^[a-zA-Z]/)
                    exit
                    else
                    print $0


                    '


                    Hope this will help some one.






                    share|improve this answer




















                    • Simpler: xrandr | awk -v monitor="^$MONITOR connected" '/disconnected/ p = 0 $0 ~ monitor p = 1 p'
                      – muru
                      May 15 at 14:02










                    • yeah, very much simpler, can you repost it as answer, to be selected as solution. thanks.
                      – Lee
                      May 16 at 3:57












                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    Well, figure it out,



                    #!/bin/bash
                    #usage the-script XRANDR_OUTPUT_NAME
                    #e.g the-script HDMI1
                    MONITOR=$1;
                    xrandr | grep -v disconnected |
                    awk '
                    if(/^'$MONITOR' connected/)
                    print $0;
                    m="t";
                    else if(m == "t")
                    if (/^[a-zA-Z]/)
                    exit
                    else
                    print $0


                    '


                    Hope this will help some one.






                    share|improve this answer












                    Well, figure it out,



                    #!/bin/bash
                    #usage the-script XRANDR_OUTPUT_NAME
                    #e.g the-script HDMI1
                    MONITOR=$1;
                    xrandr | grep -v disconnected |
                    awk '
                    if(/^'$MONITOR' connected/)
                    print $0;
                    m="t";
                    else if(m == "t")
                    if (/^[a-zA-Z]/)
                    exit
                    else
                    print $0


                    '


                    Hope this will help some one.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered May 15 at 13:44









                    Lee

                    21029




                    21029











                    • Simpler: xrandr | awk -v monitor="^$MONITOR connected" '/disconnected/ p = 0 $0 ~ monitor p = 1 p'
                      – muru
                      May 15 at 14:02










                    • yeah, very much simpler, can you repost it as answer, to be selected as solution. thanks.
                      – Lee
                      May 16 at 3:57
















                    • Simpler: xrandr | awk -v monitor="^$MONITOR connected" '/disconnected/ p = 0 $0 ~ monitor p = 1 p'
                      – muru
                      May 15 at 14:02










                    • yeah, very much simpler, can you repost it as answer, to be selected as solution. thanks.
                      – Lee
                      May 16 at 3:57















                    Simpler: xrandr | awk -v monitor="^$MONITOR connected" '/disconnected/ p = 0 $0 ~ monitor p = 1 p'
                    – muru
                    May 15 at 14:02




                    Simpler: xrandr | awk -v monitor="^$MONITOR connected" '/disconnected/ p = 0 $0 ~ monitor p = 1 p'
                    – muru
                    May 15 at 14:02












                    yeah, very much simpler, can you repost it as answer, to be selected as solution. thanks.
                    – Lee
                    May 16 at 3:57




                    yeah, very much simpler, can you repost it as answer, to be selected as solution. thanks.
                    – Lee
                    May 16 at 3:57










                    up vote
                    1
                    down vote













                    With GNU awk:



                    #!/bin/sh

                    xrandr --query | gawk -v monitor="$1" '
                    $0 ~ monitor && $0 !~ /disconnected/
                    do print
                    while (getline > -1 && $0 ~ /^[[:blank:]]/)

                    '


                    Usage:



                    $ ./the_script LVDS-1
                    LVDS-1 connected primary 1440x900+0+0 (normal left inverted right x axis y axis) 303mm x 190mm
                    1440x900 60.00*+ 59.89 50.00
                    1360x768 59.80 59.96
                    1152x864 60.00
                    1024x768 60.04 60.00
                    960x720 60.00
                    928x696 60.05
                    896x672 60.01
                    960x600 60.00
                    960x540 59.99
                    800x600 60.00 60.32 56.25
                    840x525 60.01 59.88
                    800x512 60.17
                    700x525 59.98
                    640x512 60.02
                    720x450 59.89
                    640x480 60.00 59.94
                    680x384 59.80 59.96
                    576x432 60.06
                    512x384 60.00
                    400x300 60.32 56.34
                    320x240 60.05



                    If you don't have GNU awk, you may need to replace [[:blank:]] with [ t]






                    share|improve this answer




















                    • yes, another solution. thanks.
                      – Lee
                      May 16 at 3:58














                    up vote
                    1
                    down vote













                    With GNU awk:



                    #!/bin/sh

                    xrandr --query | gawk -v monitor="$1" '
                    $0 ~ monitor && $0 !~ /disconnected/
                    do print
                    while (getline > -1 && $0 ~ /^[[:blank:]]/)

                    '


                    Usage:



                    $ ./the_script LVDS-1
                    LVDS-1 connected primary 1440x900+0+0 (normal left inverted right x axis y axis) 303mm x 190mm
                    1440x900 60.00*+ 59.89 50.00
                    1360x768 59.80 59.96
                    1152x864 60.00
                    1024x768 60.04 60.00
                    960x720 60.00
                    928x696 60.05
                    896x672 60.01
                    960x600 60.00
                    960x540 59.99
                    800x600 60.00 60.32 56.25
                    840x525 60.01 59.88
                    800x512 60.17
                    700x525 59.98
                    640x512 60.02
                    720x450 59.89
                    640x480 60.00 59.94
                    680x384 59.80 59.96
                    576x432 60.06
                    512x384 60.00
                    400x300 60.32 56.34
                    320x240 60.05



                    If you don't have GNU awk, you may need to replace [[:blank:]] with [ t]






                    share|improve this answer




















                    • yes, another solution. thanks.
                      – Lee
                      May 16 at 3:58












                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    With GNU awk:



                    #!/bin/sh

                    xrandr --query | gawk -v monitor="$1" '
                    $0 ~ monitor && $0 !~ /disconnected/
                    do print
                    while (getline > -1 && $0 ~ /^[[:blank:]]/)

                    '


                    Usage:



                    $ ./the_script LVDS-1
                    LVDS-1 connected primary 1440x900+0+0 (normal left inverted right x axis y axis) 303mm x 190mm
                    1440x900 60.00*+ 59.89 50.00
                    1360x768 59.80 59.96
                    1152x864 60.00
                    1024x768 60.04 60.00
                    960x720 60.00
                    928x696 60.05
                    896x672 60.01
                    960x600 60.00
                    960x540 59.99
                    800x600 60.00 60.32 56.25
                    840x525 60.01 59.88
                    800x512 60.17
                    700x525 59.98
                    640x512 60.02
                    720x450 59.89
                    640x480 60.00 59.94
                    680x384 59.80 59.96
                    576x432 60.06
                    512x384 60.00
                    400x300 60.32 56.34
                    320x240 60.05



                    If you don't have GNU awk, you may need to replace [[:blank:]] with [ t]






                    share|improve this answer












                    With GNU awk:



                    #!/bin/sh

                    xrandr --query | gawk -v monitor="$1" '
                    $0 ~ monitor && $0 !~ /disconnected/
                    do print
                    while (getline > -1 && $0 ~ /^[[:blank:]]/)

                    '


                    Usage:



                    $ ./the_script LVDS-1
                    LVDS-1 connected primary 1440x900+0+0 (normal left inverted right x axis y axis) 303mm x 190mm
                    1440x900 60.00*+ 59.89 50.00
                    1360x768 59.80 59.96
                    1152x864 60.00
                    1024x768 60.04 60.00
                    960x720 60.00
                    928x696 60.05
                    896x672 60.01
                    960x600 60.00
                    960x540 59.99
                    800x600 60.00 60.32 56.25
                    840x525 60.01 59.88
                    800x512 60.17
                    700x525 59.98
                    640x512 60.02
                    720x450 59.89
                    640x480 60.00 59.94
                    680x384 59.80 59.96
                    576x432 60.06
                    512x384 60.00
                    400x300 60.32 56.34
                    320x240 60.05



                    If you don't have GNU awk, you may need to replace [[:blank:]] with [ t]







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered May 15 at 14:02









                    steeldriver

                    62.2k1196164




                    62.2k1196164











                    • yes, another solution. thanks.
                      – Lee
                      May 16 at 3:58
















                    • yes, another solution. thanks.
                      – Lee
                      May 16 at 3:58















                    yes, another solution. thanks.
                    – Lee
                    May 16 at 3:58




                    yes, another solution. thanks.
                    – Lee
                    May 16 at 3:58












                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1036520%2fparse-xrandr-output-to-get-available-resolution%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    GRUB: Fatal! inconsistent data read from (0x84) 0+xxxxxx

                    `kcmshell` modules relation with `/usr/share/applications`

                    How to enroll fingerprints to Ubuntu 17.10 with VFS491