BASH Regular expressions
scwxx77
Posts: 1,469
I trying to do exercise 25.2 from 'UNIX and Linux System Administration Handbook' but I'm having trouble with the regular expression.
It's driving me nuts. Does anyone know where I'm going wrong?
E25.2 Write a shell command line or script to parse the output of xdpyinfo and print the current screen resolution in the format XxY, e.g., 1024X768.
$ xdpyinfo | grep -i dimensions #returns the following: dimensions: 1208x960 pixels (338x253 millimeters) $ xdpyinfo | grep -i dimensions | sed -n 's/\([0-9]+[x][0-9]+\)/\1/p' # returns nothing
It's driving me nuts. Does anyone know where I'm going wrong?
Winner: PTP Vuelta 2007
0
Comments
-
Yes, the first place I'd come to solve regex problems on *nix would be the road/cake stop subsection on a cycling forum.
0 -
take the easy way out...
xdpyinfo | grep -i dimensions | cut -d' ' -f7my bike - faster than god's and twice as shiny0 -
Anyway, think you've already solved it really? Yes you have additional data (in mm), but you still have answered the question.0
-
-
Awesome, sungod.
I'd still like to know why my answer returns nothing though. I'd really rather not register to another forum when there are obviously knowledgeable people on here.Winner: PTP Vuelta 20070 -
afaik use of + to denote one or more of the preceding character isn't in all implementations
using [0-9][0-9]* is more reliable to match one or more decimal digits
tbh i find sed a pain, i rarely used it in 25+ years of scripting, preferred awk
if you really want to use sed, this should work...
xdpyinfo | sed -n 's/[ ]*dimensions[^0-9]*\([0-9][0-9]*x[0-9][0-9]*\).*/\1/p'
method: match/discard the initial non-numerics, match the wanted bit and stash it in 1, match/discard the unwanted remaindermy bike - faster than god's and twice as shiny0 -
Excellent, I'll sleep better tonight! Thanks.Winner: PTP Vuelta 20070
-
Pour petrol on the keyboard and fling a lit match on it. These computers need a bit of teaching.- - - - - - - - - -
On Strava.{/url}0 -
Can you use Delphi to help construct it?seanoconn - gruagach craic!0