0

I am trying to get the id value but cannot. I also tried escaping " with \"

text="Id": "d246ad6f48f7"
[[ "${text}" =~ (.*)": "(.*)" ]]
echo ${BASH_REMATCH[2]}
Sriram Chowdary
  • 73
  • 1
  • 1
  • 10
  • 2
    In the future please first search to see if you question has already been asked before. Does this answer your question? [bash regex with quotes?](https://stackoverflow.com/questions/218156/bash-regex-with-quotes) – Inigo Jul 07 '21 at 06:56

1 Answers1

3

the call text="Id": "d246ad6f48f7" returns an error d246ad6f48f7: command not found. You should call text='"Id": "d246ad6f48f7"' instead. But it is hard to match " in bash regex. I recommend using sed like this:

echo '"Id": "d246ad6f48f7"' | sed -E '{s/"(.*)": "(.*)"/\2/}'
d246ad6f48f7

Explanation for sed:

  • -E extented Regular Expression
  • \2 second backreference
Oliver Gaida
  • 1,722
  • 7
  • 14