0

I wrote regex to get chartname(auth-token-service)). But this seems very crude, can someone write a more precise way.

chartname=`echo my-auth-token-service=xxx.azurecr.io/auth-token-service:latest | cut -d= -f1 | sed -e "s/^.*-//"`
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Can you clarify exactly what you are trying to extract here? – Kelvin Jul 10 '20 at 04:49
  • i want to get chartname should print auth-token-service – Vijay Verma Jul 10 '20 at 04:52
  • It's not clear what's wrong with this approach. – Adam Smith Jul 10 '20 at 04:54
  • I don't feel it's much better, but you can try `sed "s/.*\/\(.*\):.*/\1/"` if you just want sed – Kelvin Jul 10 '20 at 05:00
  • I want very clean, Like the use of sed not like cut and then sed. The reason this is not much readable.I am running in loop and want to get chartname ,reponame and tagName. $i value is "lsr-auth-token-service=xxx.azurecr.io/auth-token-service:latest" chartNmae= auth-token-service reponame=xxx.azurecr.io tagname=latest chartName=`echo $i | cut -d= -f1 | sed -e "s/^.*-//"` repoName=`echo $i | cut -d= -f2 | cut -d/ -f2 | cut -d: -f1` tagName=`echo $i | cut -d= -f2 | cut -d/ -f2 | cut -d: -f2` – Vijay Verma Jul 10 '20 at 05:04
  • Possible duplicate of https://stackoverflow.com/questions/13242469/how-to-use-sed-grep-to-extract-text-between-two-words – tripleee Jul 10 '20 at 08:21

5 Answers5

2

Gets text between '=' and '/'

sed "s/.*=\(.*\)\/.*/\1/" = xxx.azurecr.io

Gets text between '/' and ':'

sed "s/.*\/\(.*\):.*/\1/" = auth-token-service

Gets text after ':'

sed "s/.*:\(.*\)/\1/" = latest

Kelvin
  • 574
  • 3
  • 13
0

Not familiar with the format of token, but if I understood correctly you just want the part after the slash and before the colon.

echo my-auth-token-service=xxx.azurecr.io/auth-token-service:latest | sed -e 's/^.\+\/\([^\/]\+\):[^:]\+$/\1/'
JD Frias
  • 4,418
  • 3
  • 21
  • 24
0

The Unix shell has parameter expansion built in. You can't nest these, so it takes multiple steps, but you avoid the overhead of starting multiple external processes.

var='my-auth-token-service=xxx.azurecr.io/auth-token-service:latest'
chartname=${var%%=*}
chartname=${chartname#*-}

The suffix operator ${var%pattern} returns the value of $var with any suffix matching pattern removed; the ${var#pattern} operator does the same for a prefix match. Doubling the operator changes it to trim the longest possible pattern match instead of the shortest. (These are shell glob patterns, not regular expressions, though.)

If you require a one-liner, you can refactor the cut into the sed script.

chartname=$(sed 's/[^-]*\([^=]*\)=.*/\1/' <<< 'my-auth-token-service=xxx.azurecr.io/auth-token-service:latest')

Notice the modernized syntax $(cmd ...) over the obsolescent `cmd ...` and the Bash "here string" with <<< (not POSIX-compatible though).

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

Since you asked for a regex solution:

string=my-auth-token-service=xxx.azurecr.io/auth-token-service:latest
[[ $string =~ /([^:]*) ]] && chartname=${BASH_REMATCH[1]}

This assumes that the chartname is always between the / and the :. Note that chartname would be unassigned with this, if the reges does not match.

user1934428
  • 19,864
  • 7
  • 42
  • 87
0

With awk only tested on the GNU variant.

var=my-auth-token-service=xxx.azurecr.io/auth-token-service:latest

echo "$var" | awk -F'[=:/]' -vOFS='\n' '{print $1, $2, $3, $NF}'

Output

my-auth-token-service
xxx.azurecr.io
auth-token-service
latest
Jetchisel
  • 7,493
  • 2
  • 19
  • 18
  • 1
    This extracts the image name, not the string before the equals sign. (I had a hard time understanding exactly which part the OP is trying to extract, too.) – tripleee Jul 10 '20 at 08:02
  • @tripleee, I have updated the answer, since I'm not sure either what the output is, I have every field :-). – Jetchisel Jul 10 '20 at 08:24