我的文件中包含以下日志:
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
也许某些sed
或cut
可以解决这个问题?
提前谢谢。
答案 0 :(得分:1)
不确定这是否适用于grep
:
\d{1,3}(?:\.\d{1,3}){3}
我发布了一个指向网站的链接,您可以像jsfidle一样测试正则表达式,但显然我无法发布链接,google it;)
编辑:
这里你去:
egrep -o '[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}' /var/log/auth.log >> test.log
答案 1 :(得分:1)
将grep
语句替换为以下之一..
使用-E
选项将PATTERN解释为扩展正则表达式。 -o
选项将仅显示与PATTERN匹配的匹配行的部分。
grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' log
或使用-P
选项将PATTERN解释为Perl正则表达式
grep -Po '(?:[0-9]{1,3}\.){3}[0-9]{1,3}' log
直播查看此正则表达式。 regular expression demo
答案 2 :(得分: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]»
答案 3 :(得分:0)
只需使用awk并分为字符{
,:
,}
和空格。然后计算字段:
awk -F'[{:}[:blank:]]+' '{ print $9, $11 }' infile
它产生:
10.20.21.106 10.20.21.27
答案 4 :(得分: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})}