Valentin H
Valentin H

Reputation: 7448

Remove substring till first Token using regexp

I have the Path:

GarbageContainingSlashesAndDots/TOKEN/xyz/TOKEN/abc

How coukt I remove GarbageContainingSlashesAndDots? I know, it is before TOKEN, but Unfortunately, there are two substrings TOKEN in string. using sed s/.*TOKEN// makes my string to /abc, but I need /TOKEN/xyz/TOKEN/abc Thank You!!!

Upvotes: 2

Views: 911

Answers (7)

user332325
user332325

Reputation:

echo "./a//...b/TOKEN/abc/TOKEN/xyz"|sed 's#.*\(/TOKEN/.*/TOKEN/.*\)#\1#'

Upvotes: 0

mohit6up
mohit6up

Reputation: 4348

Using grep:

word='GarbageContainingSlashesAndDots/TOKEN/xyz/TOKEN/abc'
echo $word | grep -o '/.*'

Upvotes: 0

Valentin H
Valentin H

Reputation: 7448

Thank you for all suggestions, I've learnt something new. Finally I was able to reach my goal using grep -o

echo "GarbageContainingSlashesAndDots/TOKEN/xyz/TOKEN/abc" | grep -o "/TOKEN/.*/TOKEN/.*"

Upvotes: 0

kurumi
kurumi

Reputation: 25609

I think you have bash, so it can be a simple as

$ s="GarbageContainingSlashesAndDots/TOKEN/xyz/TOKEN/abc"
$ echo ${s#*/}
TOKEN/xyz/TOKEN/abc

or if you have Ruby(1.9+)

echo $s | ruby -e 'print gets.split("/",2)[-1]'

Upvotes: 0

Dennis Williamson
Dennis Williamson

Reputation: 360325

Divide and conquer:

$ echo 'Garbage.Containing/Slashes/And.Dots/TOKEN/xyz/TOKEN/abc' |
      sed -n 's|/TOKEN/|\n&|;s/.*\n//;p'
/TOKEN/xyz/TOKEN/abc

Upvotes: 3

aorcsik
aorcsik

Reputation: 15552

UPDATE 2: have you tried this?

s!.*\(/TOKEN.+TOKEN.*\)!\1!

UPDATE: sorry, non-greedy matches are not supported by sed

Try this:

s/.*?TOKEN//

.*? matches only for the first occurance of TOKEN.

Upvotes: -1

eumiro
eumiro

Reputation: 213005

Is perl instead of sed allowed?

perl -pe 's!.*?(?=/TOKEN)!!'


echo 'GarbageContainingSlashesAndDots/TOKEN/xyz/TOKEN/abc' | perl -pe 's!.*?(?=/TOKEN)!!'
# returns:
/TOKEN/xyz/TOKEN/abc

Sed does not support non-greedy matching. Perl does.

Upvotes: 2

Related Questions