linux - 从 linux 中的 url 中删除一个特定的数字

标签 linux awk

我有一个由特定进程生成的带有以下 header 的文件

Link: <https://rnd.corp.zoom/api/v3/repositories/99/issues?state=all&per_page=100&page=2>; rel="next", <https://rnd.corp.zoom/api/v3/repositories/99/issues?state=all&per_page=100&page=8>; rel="last"

我只想从上面的内容中的 page=8 中删除数字 8。怎么办?感谢任何帮助。

最佳答案

试试这个 -

$ cat  f
Link: <https://rnd.corp.zoom/api/v3/repositories/99/issues?state=all&per_page=100&page=2>; rel="next", <https://rnd.corp.zoom/api/v3/repositories/99/issues?state=all&per_page=100&page=8>; rel="last"

$ awk -F'[&=<>]' '{for(i=1;i<=NF;i++) if($i ~ /^page$/) {print $(i+1)}}' f
2
8

如果它被追加,那么您将使用下面的 awk 获得最后一个值:

$ awk -F'[&=<>]' '{for(i=1;i<=NF;i++) if($i ~ /^page$/) {kk=$(i+1)}} END{print kk}' ff
8

Limitation : Currently you have page=2 and page=8 and above command will print the last page value.

如果你总是想打印第二个值“8”(添加额外的行到现有的url,考虑到它会继续增加,你总是需要第二个值,然后在下面使用) -

$ cat f
Link: <https://rnd.corp.zoom/api/v3/repositories/99/issues?state=all&per_page=100&page=2>; rel="next", <https://rnd.corp.zoom/api/v3/repositories/99/issues?state=all&per_page=100&page=8>; rel="last"
 <https://rnd.corp.zoom/api/v3/repositories/99/issues?state=all&per_page=100&page=8>; rel="last"

$ awk -v k=1 -F'[&=<>]' '{for(i=1;i<=NF;i++) if(($i ~ /^page$/) && (k==2) ) {print $(i+1)} k++}' f
8

关于linux - 从 linux 中的 url 中删除一个特定的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43319169/

相关文章:

python - 我希望我的代码能更好地与我的终端一起流动

linux - Cygwin 和 makefile

html - wget:以 .jpg 扩展名保存

linux - 如何使用 grep 和 sed 打印多个字符串?

linux - 使用 AWK 获取一组 COLUMNS 中的 UNIQUE VALUE 出现次数

parsing - awk 处理和解析

linux - 如何修复 "Failed to fetch"上的 "apt-get update"错误 (Docker)

linux - 同时执行 awk 和 sed 命令

linux - 如何使用 Bash 将柱状标记数据转换为 json?

linux - 将连续流通过管道传输到多个文件