LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu
User Name
Password
Ubuntu This forum is for the discussion of Ubuntu Linux.

Notices



Reply
 
Search this Thread
Old 01-25-2013, 11:38 AM   #1
sploiz
LQ Newbie
 
Registered: Apr 2012
Posts: 3

Rep: Reputation: Disabled
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.
 
Old 01-25-2013, 11:51 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Code:
xdpyinfo | awk '/dimensions/{split($2,_,"x"); printf "%dx%d\n", _[1], _[2]-50}'
 
Old 01-27-2013, 01:40 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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.

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

Here are a few useful sed references:
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

=====
Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one-liners-explained
 
Old 01-28-2013, 12:34 PM   #4
sploiz
LQ Newbie
 
Registered: Apr 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
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.
 
Old 01-31-2013, 03:05 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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.

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:

Code:
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:
http://mywiki.wooledge.org/RegularExpression
http://www.grymoire.com/Unix/Regular.html
http://www.regular-expressions.info/
 
Old 02-01-2013, 11:22 AM   #6
sploiz
LQ Newbie
 
Registered: Apr 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Subtracting the value of a specific cell to the whole column udiubu Programming 13 11-11-2011 11:50 AM
finding screen resolution in xfce ekamlesh Linux - Newbie 2 01-09-2009 04:37 AM
Subtracting numbers in a string using C# mrobertson Programming 6 01-11-2006 03:30 PM
Subtracting the day of two dates and recieving an integer answer(VB.NET) mrobertson Programming 1 08-15-2005 02:46 PM
Subtracting dates chrisk5527 Linux - General 6 06-10-2004 03:57 PM


All times are GMT -5. The time now is 10:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration