4

シェル/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
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 に答える