如何grep文件中的URL? [英] How to grep for a URL in a file?

查看:120
本文介绍了如何grep文件中的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个很大的HTML文件,其中包含img URL: http://ex.example.com/hIh39j+ud9wr4/Uusfh.jpeg

For example, I have a huge HTML file that contains img URL: http://ex.example.com/hIh39j+ud9wr4/Uusfh.jpeg

我想获取此URL,假设它是整个文件中的 only URL.

I want to get this URL, assuming it's the only url in the entire file.

cat file.html | grep -o 'http://ex[a-zA-Z.-]*/[a-zA-Z.-]*/[a-zA-Z.,-]*'

如果URL没有加号,则此操作.

This works only if the URL doesn't have the plus signs.

我也该如何为+号工作?

How do I make work for + signs as well?

推荐答案

您错过了字符类0-9 (也无用的猫类):

grep -o 'http://ex[a-zA-Z.-]*/[a-zA-Z0-9+-]*/[a-zA-Z0-9.,-+]*' file.html

有轻微改进,对于大小写不敏感,请使用-i,并且仅匹配.jpg.jpeg的图像.

Slight improvement, use -i for case insensitivity and only match images .jpg or .jpeg.

grep -io 'http://ex[a-z.-]*/[a-z0-9+-]*/[a-z0-9.,-+]*[.jpe?g]' file.html

或者只是:

grep -io 'http://ex.example.*[.jpe?g]' file.html

这篇关于如何grep文件中的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆