我尝试用sed解析log prase:
echo 195.236.222.1 - - [24/Jul/2012:07:35:25 +0300] "GET / HTTP/1.1" 200 387 "http://www.google.fi/url?sa=t&rct=j&q=tarinat&source=web&cd=9&ved=0CGoQFjAI&url=http%3A%2F%2Fwww.suomi24.fi%2F&ei=XyQOUKi0CeWA4gTjz4D4Cg&usg=AFQjCNE6wg5zPXup3d3PRoqU-BtpiNCccw" "Mozilla/5.0 (Windows NT 6.1; rv:13.0) Gecko/20100101 Firefox/13.0.1" |
sed -r 's/.*(\&q=.*)\&.*/\1/'
我想得到“& q = tarinat”但遗憾的是:
\&q=tarinat&source=web&cd=9&ved=0CGoQFjAI&url=http%3A%2F%2Fwww.suomi24.fi%2F&ei=XyQOUKi0CeWA4gTjz4D4Cg
不明白为什么我把整个字符串直到最后。任何帮助或提示都将受到高度赞赏。
答案 0 :(得分:3)
正则表达式.*
贪婪。你不希望它变得贪婪,所以你应该写:
sed -r 's/.*(\&q=[^&]*)\&.*/\1/'
答案 1 :(得分:3)
.*
非常贪婪。您可以将其替换为负字符匹配[^&]*
,该字符匹配除了&
字符之外的任何内容
echo 195.236.222.1 - - [24/Jul/2012:07:35:25 +0300] "GET / HTTP/1.1" 200 387 "http://www.google.fi/url?sa=t&rct=j&q=tarinat&source=web&cd=9&ved=0CGoQFjAI&url=http%3A%2F%2Fwww.suomi24.fi%2F&ei=XyQOUKi0CeWA4gTjz4D4Cg&usg=AFQjCNE6wg5zPXup3d3PRoqU-BtpiNCccw" "Mozilla/5.0 (Windows NT 6.1; rv:13.0) Gecko/20100101 Firefox/13.0.1" |
sed -r 's/.*(\&q=[^&]*)\&.*/\1/'
答案 2 :(得分:1)
使用grep
的简单方法:
grep -o "&q=[^&]*"
结果:
&q=tarinat