Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: trouble using sed

  1. #1
    Join Date
    Aug 2012
    Beans
    623

    trouble using sed

    Hello,
    I have tried to get the part of a string between two words/characters using sed -n but I'm not getting the desired output


    I have a string as follows
    Code:
    [INFO_|02/01 06:51:15|Data.logInfo:1447] gprmc: [192.168.0.102] POST: http://192.168.0.106:8080/gprmc/Data acct=test01&dev=test01&gprmc=$GPRMC,012113,A,1257.604403,N,07742.223007,E,0000,000.0,010214,,*33 [acct=test01&dev=test01&gprmc=$GPRMC,012113,A,1257.604403,N,07742.223007,E,0000,000.0,010214,,*33]

    I want to get only the parts between(and including)http to *33
    So basically the output should be like
    Code:
    http://192.168.0.106:8080/gprmc/Data acct=test01&dev=test01&gprmc=$GPRMC,012113,A,1257.604403,N,07742.223007,E,0000,000.0,010214,,*33

    However, when I try with the following, it gives me a different output. And firstly works only if I remove all the spaces, something I'm not allowed to do.
    Code:
    echo "[INFO_|02/01 06:51:15|Data.logInfo:1447]gprmc:[192.168.0.102]POST:http://192.168.0.106:8080/gprmc/Dataacct=test01&dev=test01&gprmc=$GPRMC,012113,A,1257.604403,N,07742.223007,E,0000,000.0,010214,,*33[acct=test01&dev=test01&gprmc=$GPRMC,012113,A,1257.604403,N,07742.223007,E,0000,000.0,010214,,*33]"|sed -n 's/.*POST\([^ ]*\)acct/\1/p'

    Thanks

  2. #2
    Join Date
    Mar 2010
    Location
    India
    Beans
    8,175
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: trouble using sed

    This worked here -
    Code:
    sed -r 's_.*POST: (http://.*,.33) .*_\1_'
    Test command -
    Code:
    echo "[INFO_|02/01 06:51:15|Data.logInfo:1447] gprmc: [192.168.0.102] POST: http://192.168.0.106:8080/gprmc/Data acct=test01&dev=test01&gprmc=$GPRMC,012113,A,1257.604403,N,07742.223007,E,0000,000.0,010214,,*33 [acct=test01&dev=test01&gprmc=$GPRMC,012113,A,1257.604403,N,07742.223007,E,0000,000.0,010214,,*33]" | sed -r 's_.*POST: (http://.*,.33) .*_\1_'
    Result output -
    Code:
    http://192.168.0.106:8080/gprmc/Data acct=test01&dev=test01&gprmc=,012113,A,1257.604403,N,07742.223007,E,0000,000.0,010214,,*33
    If you don't use -r switch (extended regexp), you'll have to escape the braces like you did originally (that is, "\(" and "\)" )

    The problem in your filter is that you have told sed to match EVERYTHING in-between the given expressions EXCEPT SPACES. It is generously doing so
    Varun
    Help others by marking threads as [SOLVED], if they are. (See how)
    Wireless Script | Use Code Tags
    Am I not replying you? Perhaps this is why.

  3. #3
    Join Date
    Aug 2012
    Beans
    623

    Re: trouble using sed

    Thanks a lot varunendra
    I tried it out and it works for me. Let me spend some more time on it and get back to you if I still have not got it.

  4. #4
    Join Date
    Mar 2010
    Location
    India
    Beans
    8,175
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: trouble using sed

    Sure, I'd love to explain to the extent where I get confused myself

    But if you sort it out all by yourself, without risking an explanation from me, then please consider marking the thread as [SOLVED].
    Varun
    Help others by marking threads as [SOLVED], if they are. (See how)
    Wireless Script | Use Code Tags
    Am I not replying you? Perhaps this is why.

  5. #5
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,294
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: trouble using sed

    sed is overkill here, use cut (or maybe awk).
    「明後日の夕方には帰ってるからね。」


  6. #6
    Join Date
    Aug 2012
    Beans
    623

    Re: trouble using sed

    Quote Originally Posted by varunendra View Post
    This worked here -
    Code:
    sed -r 's_.*POST: (http://.*,.33) .*_\1_'
    varunendra,
    I am reading some sed tutorials and have understood a few things, but let me put my queries here.
    I'm aware that the 2nd and 3rd points are quite an ask, you can point me somewhere in case they sound crazy

    1. sed -r 's_.*POST: (http://.*,.33) .*_\1_'
    -r Extended Regex to escape the braces(quoting you)
    s The following regex is a substitution? (But what are we substituting ?)
    * Does it mean start with and end with ? But hmm, can do with some explanation here
    _\1_ I read somewhere, " Replace anything that matches the blue part with the saved capture" but couldn't make sense of it

    2. Also, to the original method, is there a way to replace the hard coded word 33 with some other pattern; the reason being, the actual format would be something like
    Code:
    [INFO_|02/01 06:51:15|Data.logInfo:1447] gprmc: [192.168.0.102] POST: http://192.168.0.106:8080/gprmc/Data acct=test01&dev=test01&gprmc=$GPRMC,012113,A,1257.604403,N,07742.223007,E,0000,000.0,010214,,*SOMEHEXADECIMAL [acct=test01&dev=test01&gprmc=$GPRMC,012113,A,1257.604403,N,07742.223007,E,0000,000.0,010214,,*SOMEHEXADECIMAL]
    Note : SOMEHEXADECIMAL can be anything say, 33, or it can be 3a or 4b etc..

    3. May I ask one more question.
    To the output below, I want to : (please see characters in green in the string below)
    A.Replace the space between 'Data' and 'acct' with a question mark
    B.Add a '\' before the word '$GPRMC' because it keeps getting trimmed off because of the '$'
    Code:
    http://192.168.0.106:8080/gprmc/Data?acct=test01&dev=test01&gprmc=\$GPRMC,012113,A,1257.604403,N,07742.223007,E,0000,000.0,010214,,*33
    Would cutting the strings at the desired location, followed by concatenating part1 + '?' + part2 be the correct approach ? Or is there a better way ?

    Thanks.
    Last edited by IAMTubby; February 5th, 2014 at 04:30 AM.

  7. #7
    Join Date
    Aug 2012
    Beans
    623

    Re: trouble using sed

    Okay Bachstelze, will keep that in mind.
    I've just started spending some time on sed. I'll go over to cut/awk or do you think I should look at it right away ?

  8. #8
    Join Date
    Aug 2012
    Beans
    623

    Re: trouble using sed

    Quote Originally Posted by Bachstelze View Post
    sed is overkill here, use cut (or maybe awk).
    Okay Bachstelze, will keep that in mind.
    I've just started spending some time on sed. I'll go over to cut/awk or do you think I should look at it right away ?

  9. #9
    Join Date
    Apr 2012
    Beans
    6,109

    Re: trouble using sed

    How about something like

    Code:
    awk 'BEGIN{OFS="?"} {sub("[$]","\\$",$7); print $6,$7}'
    e.g.
    Code:
    $ s='[INFO_|02/01 06:51:15|Data.logInfo:1447] gprmc: [192.168.0.102] POST: http://192.168.0.106:8080/gprmc/Data acct=test01&dev=test01&gprmc=$GPRMC,012113,A,1257.604403,N,07742.223007,E,0000,000.0,010214,,*33 [acct=test01&dev=test01&gprmc=$GPRMC,012113,A,1257.604403,N,07742.223007,E,0000,000.0,010214,,*33]'
    
    
    $ awk 'BEGIN{OFS="?"} {sub("[$]","\\$",$7); print $6,$7}' <<< "$s"
    http://192.168.0.106:8080/gprmc/Data?acct=test01&dev=test01&gprmc=\$GPRMC,012113,A,1257.604403,N,07742.223007,E,0000,000.0,010214,,*33

  10. #10
    Join Date
    Aug 2012
    Beans
    623

    Re: trouble using sed

    Quote Originally Posted by steeldriver View Post
    How about something like
    Code:
    awk 'BEGIN{OFS="?"} {sub("[$]","\\$",$7); print $6,$7}'
    Thanks steeldriver, that worked, but how come this doesn't work
    Code:
    $ awk 'BEGIN{OFS="?"} {sub("[$]","\\$",$7); print $6,$7}' <<< "[INFO_|02/01 06:51:15|Data.logInfo:1447] gprmc: [192.168.0.102] POST: http://192.168.0.106:8080/gprmc/Data acct=test01&dev=test01&gprmc=$GPRMC,012113,A,1257.604403,N,07742.223007,E,0000,000.0,010214,,*33 [acct=test01&dev=test01&gprmc=$GPRMC,012113,A,1257.604403,N,07742.223007,E,0000,000.0,010214,,*33]"
    The output being the following, notice the missing $GPRMC after the word gprmc.
    Code:
    http://192.168.0.106:8080/gprmc/Data?acct=test01&dev=test01&gprmc=,012113,A,1257.604403,N,07742.223007,E,0000,000.0,010214,,*33
    Would it be possible to give me some explanation, so I can get started ?

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

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