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/