Find the answer to your Linux question:
Results 1 to 4 of 4
Hi all. I need to get the substring PRUEBAS_RMAN_PARCHES from the following string "BP Key: 47531 Status: AVAILABLE Tag: PRUEBAS_RMAN_PARCHES" But the position and length may vary between different strings. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2009
    Posts
    4

    KSH: how to find the position of a substring in a string


    Hi all.
    I need to get the substring PRUEBAS_RMAN_PARCHES from the following string
    "BP Key: 47531 Status: AVAILABLE Tag: PRUEBAS_RMAN_PARCHES"
    But the position and length may vary between different strings.
    The string will always be preceded by the other string "Tag: ", so I'd like to use it as starting point
    Anybody can please tell me the easy way to do it?
    Thanks a lot.

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,440
    No idea, if it is possible with ksh alone, but sed can do it:
    Code:
    echo "BP Key: 47531 Status: AVAILABLE Tag: PRUEBAS_RMAN_PARCHES" | sed 's/\(.*\)Tag: \(.*\)/\2/'
    PRUEBAS_RMAN_PARCHES
    You must always face the curtain with a bow.

  3. #3
    Just Joined!
    Join Date
    Dec 2009
    Posts
    4
    Yes, that's exactly what I was looking for.
    Thanks

  4. $spacer_open
    $spacer_close
  5. #4
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,440
    Be aware though, that this simple regex will take anything after "Tag: " until the end of the line.
    If there are other columns following, you probably need a more complex one that matches all the cases of your data.
    You must always face the curtain with a bow.

Posting Permissions

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