0

Here is my string LIBRARY_TRGT_CANV,CANV_MATCH<anything>

I wish to get all the text after last occurance of our delimiter which is _TRGT_:

Desired output:

CANV,CANV_MATCH<anything>

------ OR -----

get all the text after second underscore i.e CANVL,CANVL_BATCH<anything>

1
  • 1
    Isn't this a subtask of your recent question here and here? I suggest you focus into one post to get actual help.
    – thanasisp
    Apr 24 at 23:06

2 Answers 2

3

No sed or awk needed for that; you can do it in any pure POSIX shell.

Assuming you have the string as variable $string, you can get everything after the last _TRGT_ with:

cutstring="${string##*_TRGET_}"

(## to remove the longest possible match at the start of the string)

To get everything after 2nd underscore:

cutstring="${string#*_*_}"

(# to remove the shortest possible match at the start of the string)

Of course, you can do it with sed as well; to get what's after last _TRGT_:

sed 's/.*_TRGT_//' <<< "$string"

Or to get what's after the second underscore:

sed 's/^[^_]*_[^_]*_//' <<< "$string"
0
0
echo "LIBRARY_TRGT_CANV,CANV_MATCH<anything>"|awk -F "_TRGT_" '{print $NF}'

output

CANV,CANV_MATCH<anything>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.