首页 > 解决方案 > 如何 Grep 特定的 URL 目录级别?

问题描述

例如,我有一个列表:

https://google.com/one/
https://google.com/one/two/
https://google.com/oneone/
https://google.com/one/two/three/

如何grep只是一级目录?所以,我的清单会是这样的:

https://google.com/one/
https://google.com/oneone/

或者只是像这样的二级端点:

https://google.com/one/two/

谢谢!

标签: sortinggrep

解决方案


awk 将是一个更好的工具

awk -F\/ 'NF==5 && $4 ~ /one/ { print $1"/"$2"/"$3"/"$4 }' file

https://google.com/one/
https://google.com/oneone/

awk -F\/ 'NF==6 && $4 ~ /one/ { print $1"/"$2"/"$3"/"$4 }' file

https://google.com/one/two/

使用 / 作为字段分隔符并检查字段数,并且该字段 4 包含一个,打印结果。


推荐阅读