How to grep for a URL in a file?

后端 未结 2 1599
醉话见心
醉话见心 2021-01-05 05:55

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

I want to get this URL, assuming it\'s the only u

2条回答
  •  生来不讨喜
    2021-01-05 06:37

    You missed the character class 0-9 (also useless use of cat):

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

    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
    

    Or how about just:

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

提交回复
热议问题