1

I saw many examples, but for some reason it still does not work for me.

This is the command I'm executing:

NUMBER=$(docker logs vault | grep Token)
NUMBER=${NUMBER##*": "}
NUMBER=$(echo $NUMBER | sed 's/^token=(.*)$//g')
echo $NUMBER

I want to get the value after '=', which is a string basically. I tried using GREP, and other regex's but I either get nothing, or just the original string.

Please advise.

David Faizulaev
  • 4,651
  • 21
  • 74
  • 124

2 Answers2

1

With sed you can simply remove the token=, with

NUMBER=$(echo token=dsa32e3 | sed 's/^token=//g') 
echo $NUMBER

Other non-regexp based alternatives are possible, as other users pointed out.

Another fun possibility is using the negative lookbehind, not supported by sed, so I used perl.

NUMBER=$(echo token=dsa32e3 | perl -pe 's/.*(?<=token=)([a-z0-9]*)/$1/g')
echo $NUMBER
Nisba
  • 3,210
  • 2
  • 27
  • 46
1

To get text after a delimiter better to use cut instead of sed as in this example:

echo 'token=dsa32e3' | cut -d= -f2

dsa32e3
  • -d= sets delimiter as = for cut
  • -f1 makes cut print first field
anubhava
  • 761,203
  • 64
  • 569
  • 643