0
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 277 100 174 100 103 464 274
--:--:-- --:--:-- --:--:--
736{"result":{"progressId":"ES_7PBXiq5gg67u9Vr","percentComplete":0.0,"status":"inProg
ress"},"meta":{"requestId":"**********************","httpStatus":"200 -
OK"}}

I need to extract the highlighted part beginning with ES_

I tried using

curl <...> | sed '"progressId"\:"(ES_[A-Za-z0-9]{15})'

however it still stores the entire output in the environment variable.

AdminBee
  • 21,637
  • 21
  • 47
  • 71
  • 1
    Welcome to the site. When posting console output, please always ensure proper formatting. I tried to edit your post, but please verify that the sample output is exactly as would appear on your console so that contributors can understand the structure of the text and point you to a working regular expression. Btw, note that your `sed` command is malformed since it does not contain any command; I'm surprised you didn't get any error message ... – AdminBee May 15 '20 at 07:22
  • 1
    Please ensure that the output is properly formatted. As it stands now, it looks like you're getting broken JSON back, which I must assume means that you've botched the copy-and-paste somehow. Since you haven't copied the output properly, not even a `sed` solution can be trusted to work reliably. – Kusalananda May 15 '20 at 11:45

2 Answers2

5

Best to use a json parsing tool to parse json. jq is a popular one.

set -o pipefail # if supported by your shell
var=$(curl -s "$url" | jq -r .result.progressId) || exit

Or:

json=$(curl -s "$url") || exit
var=$(printf '%s\n' "$json" | jq -r .result.progressId) || exit

(which allows you to extract more information from that json data later on).

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
2

This should work:

curl <...> | sed -E -n 's/.*(ES[^"]+).+/\1/p'

Explanation:

  • .* means 'any char 0 or more times'
  • ES[^"]+ means 'ES followed by any char which is not " one or more times'
  • .+ means 'any char 1 or more times'
  • \1 means 'the first group', so what's inside ()
  • p means 'print only matching lines'

If you want to store it in a variable you can do it with backtick ` like this:

VAR="$(curl <...> | sed -E -n 's/.*(ES[^"]+).+/\1/p')"
Francesco
  • 808
  • 7
  • 24
  • 1
    Please note that the backtick notation for command substitutions is deprecated, and the new `var="$( ... )"`-style syntax should be used instead. – AdminBee May 15 '20 at 07:45
  • Thank you, corrected. Do you have any article that explains why is deprecated? – Francesco May 15 '20 at 07:53
  • 1
    You're welcome. If you are interested, take a look at [this post here on U&L SE](https://unix.stackexchange.com/questions/126927/have-backticks-i-e-cmd-in-sh-shells-been-deprecated) or [this one on SO](https://stackoverflow.com/questions/9449778/what-is-the-benefit-of-using-instead-of-backticks-in-shell-scripts) which contain useful background information (and also the correct interpretation of "deprecated" in this case). – AdminBee May 15 '20 at 07:57
  • this worked like a charm for me. Next I am trying to do the following: – Viraj Deshpande May 15 '20 at 13:33
  • % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 188 100 188 0 0 1323 0 --:--:-- --:--:-- --:--:-- 1333{"result":{"fileId":"71e3270d-52b5-4082-a08b-3851bc22795f","percentComplete":100.0,"status":"complete"},"meta":{"requestId":"************************","httpStatus":"200 - OK"}} – Viraj Deshpande May 15 '20 at 13:33
  • Need to extract the fileID from the above output – Viraj Deshpande May 15 '20 at 13:34
  • @VirajDeshpande make a new question – Francesco May 15 '20 at 13:57
  • @FrancescoLucianò just did - https://unix.stackexchange.com/questions/586843/how-can-i-extract-part-of-the-output-of-a-curl-command-and-assign-it-to-a-shell – Viraj Deshpande May 15 '20 at 14:37