Search code examples
linuxshellunixfull-text-search

How to extract a field from a string with bash


I have an output from a script which looks like this

[{"fileName":"animated.avi","movieId":"34802240145","url":"http://vu.mycdn.me/upload.do?sig=2223a95d7e51832a09d03aa2f23ad3dec0a2c430\u0026expires=1441573849826\u0026clientType=0\u0026id=34802240145\u0026userId=569513266321","accessRights":0}]

I need to extract the value of "url".

I tried using sed like this. Suppose the output is in temp.txt

sed  '/url/,/accessRights/p' temp.txt

Also tried using grep, but was unable to extract it.


Solution

  • How about using cut?

    cat temp.txt | cut -d ',' -f3 | cut -d '"' -f4
    

    The fist cut results in

    "url":"http://vu.mycdn.me/upload.do?sig=2223a95d7e51832a09d03aa2f23ad3dec0a2c430\u0026expires=1441573849826\u0026clientType=0\u0026id=34802240145\u0026userId=569513266321"
    

    The second one extracts the contents between '"' characters.

    Result:

    http://vu.mycdn.me/upload.do?sig=2223a95d7e51832a09d03aa2f23ad3dec0a2c430\u0026expires=1441573849826\u0026clientType=0\u0026id=34802240145\u0026userId=569513266321