使用 Grep 获取特定条目并忽略其他条目

使用 Grep 获取特定条目并忽略其他条目

有没有办法使用 grep 忽略包含 141.8. .. 的行,但获取包含 GET 的行?现在我有这个,但我一定是做错了什么

sudo grep -v '^141.8.83.213' && "GET" /home/tsec/prototype/logs/glastopf.log | sort -k4,4 | tac | sort -uk4,4 | sort -k1,2 | tail -n 10 > /home/tsec/prototype/logs/ext$

日志内容如下

2016-04-20 13:30:59,818 (glastopf.glastopf) 141.8.83.213 requested GET /favicon.ico on e1f841a092e9:80
2016-04-20 13:31:01,817 (glastopf.glastopf) 141.8.83.213 requested POST /index on e1f841a092e9:80
2016-04-20 13:31:01,855 (glastopf.glastopf) 141.8.83.213 requested GET /style.css on e1f841a092e9:80
2016-04-20 13:31:01,883 (glastopf.glastopf) 141.8.83.213 requested GET /favicon.ico on e1f841a092e9:80
2016-04-20 16:39:55,713 (glastopf.glastopf) Initializing Glastopf 3.1.3-dev using "/data/glastopf" as work directory.
2016-04-20 16:39:55,797 (glastopf.glastopf) Connecting to main database with: sqlite:///db/glastopf.db
2016-04-20 16:39:55,834 (glastopf.glastopf) Glastopf started and privileges dropped.
2016-04-20 17:54:33,857 (glastopf.glastopf) 62.210.252.43 requested GET / on de96c7b4104d:80
2016-04-20 17:54:34,101 (glastopf.glastopf) 62.210.252.43 requested GET /HNAP1/ on de96c7b4104d:80
2016-04-20 22:06:20,265 (glastopf.glastopf) Initializing Glastopf 3.1.3-dev using "/data/glastopf" as work directory.
2016-04-20 22:06:20,399 (glastopf.glastopf) Connecting to main database with: sqlite:///db/glastopf.db
2016-04-20 22:06:20,446 (glastopf.glastopf) Glastopf started and privileges dropped.
2016-04-20 22:33:23,136 (glastopf.glastopf) 74.91.23.109 requested GET / on 11bbb1d43c02:80

所以最后我想获取字符串中包含 GET 的条目,但忽略那些 IP 为 141.8.83.213 的条目

答案1

使用两个greps:

grep "GET" /home/tsec/prototype/logs/glastopf.log |  grep -vF 141.8.83.213 | ...

man grep

-F    Match using fixed strings. Treat each  pattern  specified  as  a
      string  instead  of  a  regular  expression.  If  an  input line
      contains any of the patterns as a contiguous sequence of  bytes,
      the line shall be matched. A null string shall match every line.

-v    Select  lines not matching any of the specified patterns. If the
      -v option is not specified, selected lines shall be  those  that
      match any of the specified patterns.

因此,-F让我们避免转义.,否则它会匹配任何字符。是告诉反转匹配的-v经典方法。grep

答案2

Awk 允许在正则表达式中使用逻辑运算符,因此您可以说匹配 GET 以及那些不包含 ip 的行

  awk '/GET/&&!/141\.8\.83\.213/' log. txt

答案3

单个 grep,

grep -P '^(?!.*?141\.8\.83\.213).*\bGET\b' file

演示

相关内容