-   Ubuntu (
-   -   finding the resolution and subtracting 50 from the height (

sploiz 01-25-2013 10:38 AM

finding the resolution and subtracting 50 from the height
so i'm working on a xubuntu boot cd so the resolution isn't always going to be the same. i wanted a bash script that would find the current resolution and subtract 50 from the height. this would allow me to have an rdesktop window but leave enough room for the top bar to still show.

after many hours of googling and testing this is what i came up with:

xrandr | grep *+ | awk '{print $1}' | awk -F "x" '{print $1"x"$2-50}'

it works, but i was curious if there is a better way to do it? or just other ways. i had never used awk before and i do like it.

colucix 01-25-2013 10:51 AM


xdpyinfo | awk '/dimensions/{split($2,_,"x"); printf "%dx%d\n", _[1], _[2]-50}'

David the H. 01-27-2013 12:40 PM

Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

You should almost never need to chain multiple cut/grep/sed/awk text processing commands together. One of them (usually sed or awk) will almost always be able to do everything on its own.


xrandr | awk -F '[ x]+' '/*+/ { print $2 "x" $3 - 50 }'

Here are a few useful sed references:

Here are a few useful awk references:

sploiz 01-28-2013 11:34 AM

thanks david. i thought my statement wasn't correct but i was just happy i had it working since i'm new to this. thank you for posting the extra references, i will def look into those.

David the H. 01-31-2013 02:05 PM

Actually, your command was just fine, functionwise. All I did was condense it down into a more compact and efficient form.

There was, however, one small problem point, which I carried over into my version. :doh:

The mistake is with the "*+" matching string.

In regular expressions, which both grep and awk use, these two characters have special meanings. "*" means "zero or more of the previous character", and "+" means "one or more of the previous character".

Now, as I said, in this particular case it is a minor error. Since there is no previous character in front of "*" to operate on, it's instead interpreted as a literal asterix, and in grep, the special meaning of "+" is disabled by default (you need to use the -E option, or backslash escape it, to turn it on. See the manpage for more details). So grep was matching the literal string anyway.

In my awk command, however, the "+" meaning is in effect, so my expression was matching lines with "one or more asterixes". This makes my continuation of the error the more egregious mistake. Fortunately the output of xrandr only has one line with an asterix in it, so again there was no difference in the actual outcome.

There is, however, one final problem with your version which did have the potential for serious consequences. You failed to quote the expression, which means that the shell would first try to interpret it as a globbing pattern before executing the command. As a globbing pattern it would match any filename in the PWD ending in a plus sign, and cause grep to search for the wrong string (or perhaps search the wrong input) if it found any. A rare occurrence, certainly, but not unheard of.

Anyway, to correct the error, first, always add quotes around your command expressions if they have non-alphanumeric characters in them. Second, to remove their special regex meanings, enclose them in individual bracket expressions like this:


xrandr | awk -F '[ x]+' '/[*][+]/ { print $2 "x" $3 - 50 }'

Regular expressions are another area where it really pays dividends to learn, BTW. In fact, it's probably the best value-for-time-spent you can find in scripting/programming.

Here are a few regex tutorials:

sploiz 02-01-2013 10:22 AM

in my original expression i had tried to replace the grep with awk, but ran into just what you talked about. it was using the *+ as a regular expression and i didn't know how to properly escape them. 10 years ago i knew how to use regular expressions but i stopped doing all programming for a while and forgot a whole lot. i'll have to look into the globbing as thats a new one to me.

i ended up using colucix's code because xdpyinfo executes faster than xrandr.

you have definitely given me a lot of great references to look into. thanks!

All times are GMT -5. The time now is 05:50 AM.