Home > Back-end >  Bash regex extract value inside a quotes after a specific string
Bash regex extract value inside a quotes after a specific string

Time:02-23

I'd like to extract the value of "job" inside a console text taken from curl, it's : ix90CvhtaWfMhNiIy3RDbr2lJ2pRzGxBqHSiDq5OcI

95 28.2M     0     0   95 27.0M      0  1091k  0:00:26  0:00:25  0:00:01 2910k
100 28.2M    0    52  100 28.2M      1  1108k  0:00:26  0:00:26 --:--:-- 2887k
100 28.2M    0    52  100 28.2M      1  1108k  0:00:26  0:00:26 --:--:-- 2965k
{"job":"ix90CvhtaWfMhNiIy3RDbr2lJ2pRzGxBqHSiDq5OcI"}
[Pipeline] }
[Pipeline] // dir
[Pipeline] }

I am using this command to extract the value :

curl -v --silent 'link-to-consoleText' 2>/dev/null | grep -Eo '("job":\"[^"]*\")' | head -n1 | awk -F' = ' '{ print $1 }'

Current regex use : ("job":\"[^"]*\"), but it extracted: "job":"ix90CvhtaWfMhNiIy3RDbr2lJ2pRzGxBqHSiDq5OcI".

How to extract only the value? Thanks.

CodePudding user response:

No need for grep if you are using awk, try this:

curl -v --silent 'link-to-consoleText' 2>/dev/null | awk -F'"' '/job/ {print $4}'

CodePudding user response:

If supported, using grep -P you can use \K to clear the match buffer and assert the " at the end.

Note that you don't need the capture group and you don't have to escape the double quotes.

curl -v --silent 'link-to-consoleText' 2>/dev/null | grep -Po '"job":"\K[^"]*(?=")'
  • Related