为什么这个grep命令不排除图像?

时间:2017-02-03 17:18:33

标签: regex grep

我的正则表达式已经过测试(使用在线检查程序)并且正在运行。我正在尝试从文件中获取http链接,但使用此特别排除 http链接以图像扩展名(jpg,gif)结尾;

 grep -Eo '(http|https|rtmp)://[a-zA-Z0-9:0-9./?=_@&%|()[:blank:],;-]*' | grep -v '.(?:jpg|gif|png)'

这是grep正在处理的文件;

LONDON Remote server cluster 88: taken, https://www.wiredcontent/rashida_opener_background2.jpg
http://www.8379f690ba57e0fdf8f944324a4453de67780/tsa.a3m

BRUSSELS: Hosted MPLS cluster
http://www.5b0e9bc72b989c87a9a70da7865dbb5b4aa9c086/tsa.a3m

PARIS: Hosted atrium access, https://i.gadgets360cdn.com/large/dell_UP3218K_1483646315000.gif
http://www.39415215319c9brt6b9c7d10238c19cc82e7f88b/tsa.a3m

PRAGUE: Main Office access, 
http://www.e721f751ab0936706192cf15b4632889b38f131a/tsa.a3m

我希望grep返回这个:

http://www.8379f690ba57e0fdf8f944324a4453de67780/tsa.a3m
http://www.5b0e9bc72b989c87a9a70da7865dbb5b4aa9c086/tsa.a3m
http://www.39415215319c9brt6b9c7d10238c19cc82e7f88b/tsa.a3m
http://www.e721f751ab0936706192cf15b4632889b38f131a/tsa.a3m

提前致谢。

1 个答案:

答案 0 :(得分:2)

在第二次-P调用中为Perl扩展添加grep标志对我有用:

echo "$input" | grep -Eo '(http|https|rtmp)://[a-zA-Z0-9:0-9./?=_@&%|()[:blank:],;-]*'  | grep -vP '.(?:jpg|gif|png)'

这要求您的grep支持Perl扩展程序。