4

我想用 shell/bash 脚本从字符串中提取 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
4

2 回答 2

5

试试这个:

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
于 2013-05-11T23:46:43.917 回答
0

使用grep命令,例如:

cat yourinput.txt | grep "your_regex_here"
于 2013-05-11T23:40:39.633 回答