bash 在下载页面下载第一个匹配的正则表达式
bash download the first matching regex on download page
我想获取最新的(第一个)下载 link 匹配正则表达式。
URL=https://github.com/sharkdp/bat/releases/ # Need to look at /releases/ even though the downloads are under /releases/download/$REL/$BAT
content=$(wget $URL -q -O -)
# Parse $content for string starting 'https://' and ending "_amd64.deb"
# At the moment, that will be: href="/sharkdp/bat/releases/download/v0.18.3/bat_0.18.3_amd64.deb"
# wget -O to specify the name of the file into which wget dumps the page contents, and then - to get the dump onto standard output. -q (quiet) turns off wget output.
然后我需要以某种方式 grep/匹配开始 https://
和结束 _amd64
的字符串。然后我只需要选择该列表中的第一个。
我如何以这种方式 grep / 匹配 / 选择第一项?
一旦我有了它,我就可以很容易地在页面上下载最新版本,wget -P /tmp/ $DL
有了Bash,就可以用
rx='href="(/sharkdp/[^"]*_amd64\.deb)"'
if [[ "$content" =~ $rx ]]; then
echo "${BASH_REMATCH[1]}";
else
echo "No match";
fi
# => /sharkdp/bat/releases/download/v0.18.3/bat-musl_0.18.3_amd64.deb
href="(/sharkdp/[^"]*_amd64\.deb)"
正则表达式匹配 href="
,然后捕获到组 1 (${BASH_REMATCH[1]}
) /shardp/
+ "
+ [ 以外的零个或多个字符=17=] 然后匹配 "
.
使用 GNU grep
,您可以使用
> link=$(grep -oP 'href="\K/sharkdp/[^"]*_amd64\.deb' <<< "$content" | head -1)
> echo "$link"
# => /sharkdp/bat/releases/download/v0.18.3/bat-musl_0.18.3_amd64.deb
这里,
href="\K/sharkdp/[^"]*_amd64\.deb
- 匹配 href="
,然后从匹配中删除此文本,然后匹配 /sharkdp/
+ "
以外的任何零个或多个字符,然后 _amd_64.deb
head -1
- 只保留第一个匹配。
我想获取最新的(第一个)下载 link 匹配正则表达式。
URL=https://github.com/sharkdp/bat/releases/ # Need to look at /releases/ even though the downloads are under /releases/download/$REL/$BAT
content=$(wget $URL -q -O -)
# Parse $content for string starting 'https://' and ending "_amd64.deb"
# At the moment, that will be: href="/sharkdp/bat/releases/download/v0.18.3/bat_0.18.3_amd64.deb"
# wget -O to specify the name of the file into which wget dumps the page contents, and then - to get the dump onto standard output. -q (quiet) turns off wget output.
然后我需要以某种方式 grep/匹配开始 https://
和结束 _amd64
的字符串。然后我只需要选择该列表中的第一个。
我如何以这种方式 grep / 匹配 / 选择第一项?
一旦我有了它,我就可以很容易地在页面上下载最新版本,wget -P /tmp/ $DL
有了Bash,就可以用
rx='href="(/sharkdp/[^"]*_amd64\.deb)"'
if [[ "$content" =~ $rx ]]; then
echo "${BASH_REMATCH[1]}";
else
echo "No match";
fi
# => /sharkdp/bat/releases/download/v0.18.3/bat-musl_0.18.3_amd64.deb
href="(/sharkdp/[^"]*_amd64\.deb)"
正则表达式匹配 href="
,然后捕获到组 1 (${BASH_REMATCH[1]}
) /shardp/
+ "
+ [ 以外的零个或多个字符=17=] 然后匹配 "
.
使用 GNU grep
,您可以使用
> link=$(grep -oP 'href="\K/sharkdp/[^"]*_amd64\.deb' <<< "$content" | head -1)
> echo "$link"
# => /sharkdp/bat/releases/download/v0.18.3/bat-musl_0.18.3_amd64.deb
这里,
href="\K/sharkdp/[^"]*_amd64\.deb
- 匹配href="
,然后从匹配中删除此文本,然后匹配/sharkdp/
+"
以外的任何零个或多个字符,然后_amd_64.deb
head -1
- 只保留第一个匹配。