首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

运用grep命令正则匹配文本内容 linux and unix grep command using regexp

2012-07-22 
使用grep命令正则匹配文本内容 linux and unix grep command using regexp需求:分析一段日志,摘取IP并生成

使用grep命令正则匹配文本内容 linux and unix grep command using regexp
需求:分析一段日志,摘取IP并生成文本

#cat info.log | egrep 'gateway=[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}'

这样可以列出所有匹配'gatewap=127.0.0.1'这样格式的记录,但是,我不需要整行记录,只需要匹配正则的文本,很简单,加一个参数就可以搞定

#cat info.log | egrep -o 'gateway=[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}'

需要输出文本,稍作修改如下:

#cat info.log | egrep -o 'gateway=[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}' > gateway.txt


grep命令参考:
http://www.computerhope.com/unix/ugrep.htm

正则参考:
http://www.zytrax.com/tech/web/regex.htm

热点排行