2011-09-11 41 views
0

我有一個字符串的以下模式:獲得來自字符串的子串用grep命令

"METHOD URL VERSION" 

例如"GET /someurl/resource.html HTTP/1.1"

我怎樣才能從這個字符串使用grep的URL。

我做了以下(假設字符串中包含f.txt)

cat f.txt | grep -P '[^(()|(\")|(HTTP\/\d\.\d)|(GET)|(POST))]+' -o 

,但它給了我這樣的輸出

someurl 
resource 
html 

如何取回/someurl/resource.html?

回答

1

這應做到:

grep -o "\/.* " 

好吧,如果你有一個模式:

「somestring1withoutspaces somestring2withoutspaces somestring3withoutspaces」,那麼這兩個作品:

grep -o " .* " 
awk '{print $2}' 
+0

網址可以http開頭並與www或只是像image.png – maks

+0

文件的名稱非常感謝,它幫助 – maks

+0

很高興有幫助! – ennuikiller