1

我的文件中有以下日志:

Aug 27 18:41:44 Testlab nixc[27354]: 207416484 {10.20.21.106:52907 10.20.21.27:80} http traffic

我只想提取 ips 示例:

10.22.39.106 10.242.29.27

我试过了

grep -E -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}.*[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' log

我仍然得到

10.20.21.106:52907 10.20.21.27
10.20.21.106:52907 10.20.21.27
10.20.21.106:52907 10.20.21.27
10.20.21.106:52907 10.20.21.27

也许一些sedcut会做的伎俩?

先感谢您。

4

5 回答 5

1

不确定这是否适用于grep

\d{1,3}(?:\.\d{1,3}){3}

我发布了一个指向一个站点的链接,您可以在其中测试 jsfidle 之类的正则表达式,但显然我还不能发布链接,谷歌它;)

编辑:

给你:

egrep -o '[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}' /var/log/auth.log >> test.log
于 2013-09-04T19:40:40.743 回答
1

Replace your grep statement with one of the following..

Using the -E option Interprets the PATTERN as an extended regular expression. The -o option will show only the part of a matching line that matches the PATTERN.

grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' log

or using the -P option Interprets the PATTERN as a Perl regular expression

grep -Po '(?:[0-9]{1,3}\.){3}[0-9]{1,3}' log

See this regular expression live. regular expression demo

于 2013-09-04T19:50:45.203 回答
1
grep -Eo '\<(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])' file

# \<(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])
# 
# Assert position at the beginning of a word «\<»
# Match the regular expression below and capture its match into backreference number 1 «(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])»
#    Match either the regular expression below (attempting the next alternative only if this one fails) «25[0-5]»
#       Match the characters “25” literally «25»
#       Match a single character in the range between “0” and “5” «[0-5]»
#    Or match regular expression number 2 below (attempting the next alternative only if this one fails) «2[0-4][0-9]»
#       Match the character “2” literally «2»
#       Match a single character in the range between “0” and “4” «[0-4]»
#       Match a single character in the range between “0” and “9” «[0-9]»
#    Or match regular expression number 3 below (attempting the next alternative only if this one fails) «1[0-9]{2}»
#       Match the character “1” literally «1»
#       Match a single character in the range between “0” and “9” «[0-9]{2}»
#          Exactly 2 times «{2}»
#    Or match regular expression number 4 below (the entire group fails if this one fails to match) «[1-9]?[0-9]»
#       Match a single character in the range between “1” and “9” «[1-9]?»
#          Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
#       Match a single character in the range between “0” and “9” «[0-9]»
# Match the character “.” literally «\.»
# Match the regular expression below and capture its match into backreference number 2 «(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])»
#    Match either the regular expression below (attempting the next alternative only if this one fails) «25[0-5]»
#       Match the characters “25” literally «25»
#       Match a single character in the range between “0” and “5” «[0-5]»
#    Or match regular expression number 2 below (attempting the next alternative only if this one fails) «2[0-4][0-9]»
#       Match the character “2” literally «2»
#       Match a single character in the range between “0” and “4” «[0-4]»
#       Match a single character in the range between “0” and “9” «[0-9]»
#    Or match regular expression number 3 below (attempting the next alternative only if this one fails) «1[0-9]{2}»
#       Match the character “1” literally «1»
#       Match a single character in the range between “0” and “9” «[0-9]{2}»
#          Exactly 2 times «{2}»
#    Or match regular expression number 4 below (the entire group fails if this one fails to match) «[1-9]?[0-9]»
#       Match a single character in the range between “1” and “9” «[1-9]?»
#          Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
#       Match a single character in the range between “0” and “9” «[0-9]»
# Match the character “.” literally «\.»
# Match the regular expression below and capture its match into backreference number 3 «(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])»
#    Match either the regular expression below (attempting the next alternative only if this one fails) «25[0-5]»
#       Match the characters “25” literally «25»
#       Match a single character in the range between “0” and “5” «[0-5]»
#    Or match regular expression number 2 below (attempting the next alternative only if this one fails) «2[0-4][0-9]»
#       Match the character “2” literally «2»
#       Match a single character in the range between “0” and “4” «[0-4]»
#       Match a single character in the range between “0” and “9” «[0-9]»
#    Or match regular expression number 3 below (attempting the next alternative only if this one fails) «1[0-9]{2}»
#       Match the character “1” literally «1»
#       Match a single character in the range between “0” and “9” «[0-9]{2}»
#          Exactly 2 times «{2}»
#    Or match regular expression number 4 below (the entire group fails if this one fails to match) «[1-9]?[0-9]»
#       Match a single character in the range between “1” and “9” «[1-9]?»
#          Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
#       Match a single character in the range between “0” and “9” «[0-9]»
# Match the character “.” literally «\.»
# Match the regular expression below and capture its match into backreference number 4 «(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])»
#    Match either the regular expression below (attempting the next alternative only if this one fails) «25[0-5]»
#       Match the characters “25” literally «25»
#       Match a single character in the range between “0” and “5” «[0-5]»
#    Or match regular expression number 2 below (attempting the next alternative only if this one fails) «2[0-4][0-9]»
#       Match the character “2” literally «2»
#       Match a single character in the range between “0” and “4” «[0-4]»
#       Match a single character in the range between “0” and “9” «[0-9]»
#    Or match regular expression number 3 below (attempting the next alternative only if this one fails) «1[0-9]{2}»
#       Match the character “1” literally «1»
#       Match a single character in the range between “0” and “9” «[0-9]{2}»
#          Exactly 2 times «{2}»
#    Or match regular expression number 4 below (the entire group fails if this one fails to match) «[1-9]?[0-9]»
#       Match a single character in the range between “1” and “9” «[1-9]?»
#          Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
#       Match a single character in the range between “0” and “9” «[0-9]»
于 2013-09-04T20:10:14.213 回答
0

只需使用并拆分字符{、、:}空格。然后计算字段:

awk -F'[{:}[:blank:]]+' '{ print $9, $11 }' infile

它产生:

10.20.21.106 10.20.21.27
于 2013-09-04T19:35:49.890 回答
0
    {(\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}):.+\s(\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}:.\d{1,3})}

正则表达式可视化

在 Debuggex 上实时编辑

于 2013-09-04T20:45:00.323 回答