Find the answer to your Linux question:
Results 1 to 3 of 3
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Post Remove a substring using SED


    I have a line containing
    PRODUCT NAME('MY.PRODUCT') DESCR('THIS IS THE BEST PRODUCT') WAREHOUSE('WEST') CITY(TORONTO)

    I want to remove just the DESCR field so that the data will look like
    PRODUCT NAME('MY.PRODUCT') WAREHOUSE('WEST') CITY(TORONTO)

    Command I tried
    echo "PRODUCT NAME('MY.PRODUCT') DESCR('THIS IS THE BEST PRODUCT') WAREHOUSE('WEST') CITY(TORONTO)" | sed "s/DESCR('.*') //g"

    What I got is
    PRODUCT NAME('MY.PRODUCT') CITY(TORONTO)

    The WAREHOUSE('WEST') is also gone.
    I did some google and people are saying this has to do with REGX and grouping. I just need some help on understanding this and how can I fix it.

  2. #2
    Use
    echo "PRODUCT NAME('MY.PRODUCT') DESCR('THIS IS THE BEST PRODUCT') WAREHOUSE('WEST') CITY(TORONTO)" | sed "s/DESCR('.*?') //g"

    The * quantifier matches greedy. So your pattern matches to the last ') plus a space.

    Appending a ? to the * quantifier makes it lazy AKA non-greedy.

  3. #3
    -->
    Quote Originally Posted by Linux_789 View Post
    I have a line containing
    PRODUCT NAME('MY.PRODUCT') DESCR('THIS IS THE BEST PRODUCT') WAREHOUSE('WEST') CITY(TORONTO)

    I want to remove just the DESCR field... .
    Good luck getting a sed to do non-greedy matching. IIUC GNU sed doesn't do that.

    You could use perl - pe instead:
    Code:
    perl - pe "s/DESCR\(. *?\)//"
    but unless you really have to stick to slow syntax from 30 years ago I suggest using POSIX shell syntax:
    Code:
     $ line="..." 
    $ echo "${line%DESCR('*}${line#*DESCR('*')}"
    I tested that with dash.

    HTH

    Regards, John Little

$spacer_open
$spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •