Extract url from a header using bash script

2

I have been trying for quite some time to extract part of a text (a header) in bash script but I have not succeeded, this is what I have:

link: <https://api.some.com/v1/monitor/zzsomeLongIdzz?access_token=xxSomeLongTokenxx==>; rel="monitor",<https://api.some.com/v1/services/xx/something-more/accounts/2345?access_token=xxSomeLongTokenxx==>; rel="parent"

Here with a little format so you can see it better

link: 
<https://api.some.com/v1/monitor/zzsomeLongIdzz?access_token=xxSomeLongTokenxx==>; 
rel="monitor",
<https://api.some.com/v1/services/xx/something-more/accounts/2345?access_token=xxSomeLongTokenxx==>; 
rel="parent"

I need the second part, only the url , basically the values between ,< and >; rel="parent" and assign that to a variable, something like:

my_url = $(echo $complete_header) <== aplicarle alguna forma de filtro

I have no idea how to apply a pattern or regex to extract the data I need. I used to use jq to filter json , something like:

error_message=$(echo $response | jq '.["errors"]|.[0]|.["message"]')

Unfortunately for me this is not json .

Can someone help me to go in the right direction to my question?

If the solution were with regex I would like you to give me a summary of what that regex means, enough to be able to modify it.

    
asked by Gepser 04.02.2016 в 19:32
source

1 answer

2

I have put the header in a file header that contains your example:

$ cat header
link: <https://api.some.com/v1/monitor/zzsomeLongIdzz?access_token=xxSomeLongTokenxx==>; rel="monitor",<https://api.some.com/v1/services/xx/something-more/accounts/2345?access_token=xxSomeLongTokenxx==>; rel="parent"

Using --only-matching , --perl-regexp and assertions lookbehind and lookahead ( Advanced Grep Topics ):

$ cat header | grep --only-matching --perl-regexp "(?<=,\<)(.*?)(?=\>; rel=\"parent\")"

You get:

https://api.some.com/v1/services/xx/something-more/accounts/2345?access_token=xxSomeLongTokenxx==

In your bash script:

my_url = $(echo $complete_header | grep --only-matching --perl-regexp "(?<=,\<)(.*?)(?=\>; rel=\"parent\")")

I think that for your specific case it could work very well.

    
answered by 04.02.2016 / 20:08
source