Pre*_*ti 1 scripting shell-script
我正在尝试提取脚本中响应URL中返回的一部分。这将在脚本的后期阶段使用。curlbash
卷曲响应具有以下 URL:
https://www.example.com/1234/text/111?x=999988
Run Code Online (Sandbox Code Playgroud)
现在,我想text/111?x=999988从中提取。另外,我只想将数字存储111在变量中。我正在尝试这样做
#!/bin/bash
sample_link="https://www.example.com/1234/text/111?x=999988"
sample_extract=${sample_link##*/}
echo "$sample_extract"
Run Code Online (Sandbox Code Playgroud)
但这只给了我111?x=999988
谁能告诉我我缺少什么以及如何解决它?
PS:如果缺少任何信息,请告诉我
和GNU grep:
$ grep -oP 'https://[^/]+/\d+/\K.*' <<< 'https://www.example.com/1234/text/111?x=999988'
text/111?x=999988
Run Code Online (Sandbox Code Playgroud)
或者
和Perl:
$ perl -nE 'say $& if m@https://[^/]+/\d+/\K.*@' <<< 'https://www.example.com/1234/text/111?x=999988'a
text/111?x=999988a
Run Code Online (Sandbox Code Playgroud)
| 节点 | 解释 |
|---|---|
https:// |
'https://' |
[^/]+ |
任何字符,除了:/(1 次或多次(匹配尽可能多的数量)) |
/ |
/ |
\d+ |
数字 (0-9)(1 次或多次(匹配尽可能多的数量)) |
/ |
/ |
\K |
重置比赛的开始(什么是Kept)作为使用后视断言的更短替代方案:环顾四周并支持正则表达式中的 K |
.* |
除 \n 之外的任何字符(0 次或多次(匹配尽可能多的数量)) |
$ grep -oP 'https://[^/]+/\d+/\w+/\K\d+' <<< 'https://www.example.com/1234/text/111?x=999988'
111
Run Code Online (Sandbox Code Playgroud)
或者
$ perl -nE 'say $& if m@https://[^/]+/\d+/\w+/\K\d+' <<< 'https://www.example.com/1234/text/111?x=999988'a
111
Run Code Online (Sandbox Code Playgroud)
| 节点 | 解释 |
|---|---|
https:// |
'https://' |
[^/]+ |
任何字符,除了:/(1 次或多次(匹配尽可能多的数量)) |
/ |
/ |
\d+ |
数字 (0-9)(1 次或多次(匹配尽可能多的数量)) |
/ |
/ |
\w+ |
单词字符(az、AZ、0-9、_)(1 次或多次(匹配尽可能多的数量)) |
/ |
/ |
\K |
重置比赛的开始(什么是Kept)作为使用后视断言的更短替代方案:环顾四周并支持正则表达式中的 K |
\d+ |
数字 (0-9)(1 次或多次(匹配尽可能多的数量)) |
要分配给变量,我想你知道怎么做=)(你原来的帖子展示了如何做)