Bash Sed输出url中两个匹配字符串之间的值
我有多个URL作为输入Bash Sed输出url中两个匹配字符串之间的值,bash,url,sed,grep,Bash,Url,Sed,Grep,我有多个URL作为输入 https://drive.google.com/a/domain.com/file/d/1OR9QLGsxiLrJIz3JAdbQRACd-G9ZfL3O/view?usp=drivesdk https://drive.google.com/a/domain.com/file/d/1sEWMFqGW9p2qT-8VIoBesPlVJ4xvOzXD/view?usp=drivesdk 如何创建一个sed命令来只返回文件ID 期望输出: 1OR9QLGsxiLrJIz3J
https://drive.google.com/a/domain.com/file/d/1OR9QLGsxiLrJIz3JAdbQRACd-G9ZfL3O/view?usp=drivesdk
https://drive.google.com/a/domain.com/file/d/1sEWMFqGW9p2qT-8VIoBesPlVJ4xvOzXD/view?usp=drivesdk
如何创建一个sed命令来只返回文件ID
期望输出:
1OR9QLGsxiLrJIz3JAdbQRACd-G9ZfL3O
1sEWMFqGW9p2qT-8VIoBesPlVJ4xvOzXD
看起来我需要从/d/开始到/view结束,但我不太确定如何做到这一点
我试过了
sed-e's/d\(.*)\/view/\1/'
我可以用cut-d'/'-f8
另外,
awk-F/{print$8}文件
工作正常,谢谢 您的命令几乎正确:
# Wrong
sed -e 's/d\(.*\)\/view/\1/'
# better, removing unmatched stuff including the / after the d
sed -e 's/.*d\/\(.*\)\/view.*/\1/'
# better: using # for making the command easier to read
sed -e 's#.*d/\(.*\)/view.*#\1#'
# Alternative:Using cut when you don't know which field /d/ is
some_straem | grep -Eo '/d/.*/view' | cut -d/ -f3
如果用
/
分隔时它总是第8个位置,则剪切
或awk
可能更合适。