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.

  • 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 at 7: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 at 11:45
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')"
| improve this answer | |
  • 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 at 7:45
  • Thank you, corrected. Do you have any article that explains why is deprecated? – Francesco Lucianò May 15 at 7:53
  • 1
    You're welcome. If you are interested, take a look at this post here on U&L SE or this one on SO which contain useful background information (and also the correct interpretation of "deprecated" in this case). – AdminBee May 15 at 7:57
  • this worked like a charm for me. Next I am trying to do the following: – Viraj Deshpande May 15 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 at 13:33
4

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).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.