Reputation: 7448
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
Reputation:
echo "./a//...b/TOKEN/abc/TOKEN/xyz"|sed 's#.*\(/TOKEN/.*/TOKEN/.*\)#\1#'
Upvotes: 0
Reputation: 4348
Using grep
:
word='GarbageContainingSlashesAndDots/TOKEN/xyz/TOKEN/abc'
echo $word | grep -o '/.*'
Upvotes: 0
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
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
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
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
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