BASH Regular expressions

scwxx77
scwxx77 Posts: 1,469
edited October 2012 in The cake stop
I trying to do exercise 25.2 from 'UNIX and Linux System Administration Handbook' but I'm having trouble with the regular expression.
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 :wink:

Comments

  • dodgy
    dodgy Posts: 2,890
    Yes, the first place I'd come to solve regex problems on *nix would be the road/cake stop subsection on a cycling forum.

    ;)
  • sungod
    sungod Posts: 17,430
    take the easy way out...

    xdpyinfo | grep -i dimensions | cut -d' ' -f7
    my bike - faster than god's and twice as shiny
  • dodgy
    dodgy Posts: 2,890
    Anyway, think you've already solved it really? Yes you have additional data (in mm), but you still have answered the question.
  • jim453
    jim453 Posts: 1,360
    scwxx77 wrote:
    I trying to do exercise 25.2 from 'UNIX and Linux System Administration Handbook'

    Don't.
  • Stackoverflow is what you want.
    Is the gorilla tired yet?
  • scwxx77
    scwxx77 Posts: 1,469
    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 2007 :wink:
  • sungod
    sungod Posts: 17,430
    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 remainder
    my bike - faster than god's and twice as shiny
  • scwxx77
    scwxx77 Posts: 1,469
    Excellent, I'll sleep better tonight! Thanks.
    Winner: PTP Vuelta 2007 :wink:
  • desweller
    desweller Posts: 5,175
    Pour petrol on the keyboard and fling a lit match on it. These computers need a bit of teaching.
    - - - - - - - - - -
    On Strava.{/url}
  • pinno
    pinno Posts: 52,497
    Can you use Delphi to help construct it?
    seanoconn - gruagach craic!