0

Hello I am new to linux and grep and awk commands.

I am trying to get the number value from this string variable and then store it in another variable.

var1="Version must be incremented on update. Current version is: 532"
var2=var1 | grep "?"

What should I do in order to only retrieve val 532 from the above string and store it in var2

Alex
  • 172
  • 1
  • 1
  • 8

1 Answers1

1

You need $var1 to reference it, rather than var1. You need to echo it so that Bash won't try to run it as a program. You should put double-quotes around it as well.

For grep, you need a regular expression to match the numbers, lots of patterns might work but I'm using "one or more digits, at the end of the string", and you need the grep option -o to print only the matching text, instead of the whole line.

Run this inside $() to stop the shell trying to run the result as a program, and your code looks like this:

var1="Version must be incremented on update. Current version is: 532"
var2=$(echo "$var1" | grep '[0-9]\+$' -o)
echo "$var2"
# 532
TessellatingHeckler
  • 5,726
  • 3
  • 26
  • 44