check value of json response in bash script -


{"running": 1, "finished": 3, "node_name": "l-2.local", "pending": 0, "status": "ok"} 

i'm trying parse response check value of "running" bash script. know there's libraries this, reason jq library wouldn't install, , prefer not install it, need 1 command. i'd use python instead.

i tried using command answer, on response

| grep -po '"running":"\k[^,]*'

but failed due "."

bash: {"running": 0, "finished": 0, "node_name": "l-2: command not found

is there way check value of "running" grep, , not have error?

you may use sed remove not need , capture , keep need using capturing group , backreference:

s='{"running": 1, "finished": 3, "node_name": "l-2.local", "pending": 0, "status": "ok"}' echo $s | sed 's/.*"running":[[:space:]]*\([0-9]*\).*/\1/' 

see online demo.

pattern details

  • .* - 0+ chars many possible
  • "running": - literal substring
  • [[:space:]]* - 0+ whitespaces
  • \([0-9]*\) - capturing group (note escapes, necessary since pattern posix bre compliant) matching 0 or more digits
  • .* - rest of line.

the \1 backreference group 1 value.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

.htaccess - ERR_TOO_MANY_REDIRECTS htaccess -