regex - 使用 bash 脚本从 header 中提取父 url

标签 regex bash parsing

我花了一些时间尝试在 bash 脚本上获取文本的某些部分(标题),但我做不到。这是我的字符串:

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"

这里有一些格式,这样你可以看得更清楚:

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"

我需要第二部分,就是url , 基本上是 ,< 之间的值和 >; rel="parent"并将其分配给一个变量,例如:

my_url = $(echo $complete_header) <== some way to filter that

我不知道如何应用一些正则表达式或模式来提取我需要的数据。过去我用过jq用于过滤 json回复,像这样:

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

但不幸的是,这不是 json。有人可以指出我正确的方向吗?

最佳答案

使用以下代码:

#!/bin/bash
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"'
re=",<([^>]+)>"
# look for ,< literally
# capture everything that is not a > one or more times ([^>]+)
# look for the closing > literally
my_url="test"
if [[ $link =~ $re ]]; then my_url=${BASH_REMATCH[1]}; fi
echo $my_url

参见 a demo on ideone.com .

关于regex - 使用 bash 脚本从 header 中提取父 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35208709/

相关文章:

javascript - 密码验证正则表达式之外的数字或特殊字符要求

Bash 编程 : get name of one-and-only subfolder of given directory

php - 解析位置字符串

c - C 的 XML 解析器

java - 正则表达式替代PreparedStatement IN子句?

Python RegEx,匹配字符串中的单词并获取计数

linux - Bash - curl 命令行执行不佳

c++ - 解析与char c++相关的整数

regex - 触发器中的 Postgres 正则表达式匹配不正确

bash - 如何让猫开始新的一行