使用grep命令从字符串中获取子字符串

时间:2011-09-11 21:00:52

标签: shell 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 个答案:

答案 0 :(得分:1)

这应该这样做:

grep -o "\/.* "

好的,如果您有模式:

“somestring1withoutspaces somestring2withoutspaces somestring3withoutspaces”然后这两个都有效:

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