-3

I am very new to sed.

I wanted to extract the id from

{ "requestId": "aee4f0cd-4b15-4df5-ab6b-62874963105a" } 

and return aee4f0cd-4b15-4df5-ab6b-62874963105a.

How do I do it using sed ?

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
Sriram Chowdary
  • 73
  • 1
  • 1
  • 10
  • 3
    That looks like JSON. Have you considered using `jq`? – dawg Oct 29 '21 at 13:29
  • That looks like JSON. A parser like [tag:jq] would be a better fit. `echo '{ "requestId": "aee4f0cd-4b15-4df5-ab6b-62874963105a" }' | jq -r '.requestId'` – 0stone0 Oct 29 '21 at 13:29
  • Does this answer your question? [Non greedy (reluctant) regex matching in sed?](https://stackoverflow.com/questions/1103149/non-greedy-reluctant-regex-matching-in-sed) – gkhaos Oct 29 '21 at 13:30
  • Does this answer your question? [Parsing JSON with Unix tools](https://stackoverflow.com/questions/1955505/parsing-json-with-unix-tools) – 0stone0 Oct 29 '21 at 13:51

2 Answers2

2

jq is better:

s='{ "requestId": "aee4f0cd-4b15-4df5-ab6b-62874963105a" } '

echo "$s" | jq '.requestId'
"aee4f0cd-4b15-4df5-ab6b-62874963105a"

Or, no quotes:

echo "$s" | jq -r '.requestId'
aee4f0cd-4b15-4df5-ab6b-62874963105a
dawg
  • 98,345
  • 23
  • 131
  • 206
1

Using jq is a better solution, but by sed you should apply this:

echo '{ "requestId": "aee4f0cd-4b15-4df5-ab6b-62874963105a" }' | sed -r 's/^.*: "(.+)".*$/\1/'
Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74