Reputation: 6189
I m trying to extract out the numeric (timestamp) pattern from json response in bash script:
Here is the code snippet >
response=$(curl -isb -H "Accept: application/json" http://host:5000/app?q=1)
#response=$(wget -q -O - http://host:5000/app?q=1)
echo "Response : $response"
latest=$(echo $response | grep -oP "[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{1,}")
echo "Latest version is : $latest"
However when I run the test the output of grep in empty
Response : HTTP/1.1 200 OK
Content-Type: application/json
X-Content-Type-Options: nosniff
Set-Cookie: rack.session=BAh7CEkiD3Nlc3Npb25faWQGOgZFVEkiRTMyYWM2NGJjNzBhMDczYWQ5ZDcz%0AMDQ4MjA2ZjYwMzY4NDMxMzhmYTEwY2Q2MDEwMmU5NjA0YmY4MDBmNmRhZDQG%0AOwBGSSIJY3NyZgY7AEZJIiU4OTAyMDk4MDcxOTM5MWY1MmMzMzZlZjhiNjNi%%%0ABjsARkkiGUhUVFBfQUNDRVBUX0xBTkdVQUdFBjsAVEkiLWRhMzlhM2VlNWU2%0AYjRiMGQzMjU1YmZlZjk1NjAxODkwYWZkODA3MDkGOwBG%0A--3d18f21cd9851f99ff05b45556a78988132221c9; path=/; HttpOnly
Content-Length: 40
["env-svr-app-mod-2014-05-15-155"]
Latest version is :
So the curl output has timestamp in it. I m expecting to see 2014-05-15-155 in the output.
I have tried couple of different option (and wget) but looks like I m missing something obvious here.
Is it the qupts/ brackets/ new line in the curl output itself that's causing issue? (I remember seeing this work couple of times)
Appreciate nudges! Thx.
Upvotes: 0
Views: 394
Reputation: 3622
Try quoting $response
on the line with grep. bash tends to convert newlines into spaces.
Upvotes: 1
Reputation: 7959
Replace you grep command by:
perl -lne '/\w+-(\d+-.*)"/ && print $1'
Ex:
[root@TIAGO-TEST tmp]# cat response
HTTP/1.1 200 OK
Content-Type: application/json
X-Content-Type-Options: nosniff
Set-Cookie: rack.session=BAh7CEkiD3Nlc3Npb25faWQGOgZFVEkiRTMyYWM2NGJjNzBhMDczYWQ5ZDcz%0AMDQ4MjA2ZjYwMzY4NDMxMzhmYTEwY2Q2MDEwMmU5NjA0YmY4MDBmNmRhZDQG%0AOwBGSSIJY3NyZgY7AEZJIiU4OTAyMDk4MDcxOTM5MWY1MmMzMzZlZjhiNjNi%%%0ABjsARkkiGUhUVFBfQUNDRVBUX0xBTkdVQUdFBjsAVEkiLWRhMzlhM2VlNWU2%0AYjRiMGQzMjU1YmZlZjk1NjAxODkwYWZkODA3MDkGOwBG%0A--3d18f21cd9851f99ff05b45556a78988132221c9; path=/; HttpOnly
Content-Length: 40
["env-svr-app-mod-2014-05-15-155"]
[root@TIAGO-TEST tmp]# cat response| perl -lne '/\w+-(\d+-.*)"/ && print $1'
2014-05-15-155
Upvotes: 0