如何从shell中的字符串中检索url?

时间:2013-05-11 23:36:26

标签: regex string bash shell grep

我想用shell / bash脚本从字符串中提取url,如果字符串中有多个url,则只返回第一个url。

我在下面提供了输入和输出字符串的一些示例。我猜我需要做一些正则表达式,但我不太熟悉如何在bash / shell中执行此操作?

Input: Take a look at this site: http://www.google.com/ and you'll find your answer
Output: http://www.google.com/


Input: http://www.google.com
Output: http://www.google.com


Input: Check out http://www.bing.com and http://www.google.com
Output: http://www.bing.com


Input: Grettings, visit <http://www.mywebsite.com> today!
Output: http://www.mywebsite.com

2 个答案:

答案 0 :(得分:3)

试试这个:

grep -Eo 'http://[^ >]+' yourFile|head -1 

例如:

kent$  echo "Check out http://www.bing.com and http://www.google.com"|grep -Eo 'http://[^ >]+'|head -1 
http://www.bing.com
kent$  echo "Grettings, visit <http://www.mywebsite.com> today"|grep -Eo 'http://[^ >]+'|head -1 
http://www.mywebsite.com

答案 1 :(得分:1)

使用grep命令,例如:

cat yourinput.txt | grep "your_regex_here"