less:多个过滤条件用AND

less:多个过滤条件用AND

less中如何指定与AND连接的多个过滤条件?

我想过滤行不包含“nat”但包含 IP 地址 192.168.1.1 的日志,例如:

&/!nat && 192.168.1.1

然而它不起作用,结果是空的......

请指教。

答案1

从v569版本开始,较少的可以“堆叠”过滤器以便“深入”到日志文件。

从手册页:

&pattern
              Display  only  lines which match the pattern; lines which do not
              match the pattern are not displayed.  If pattern  is  empty  (if
              you  type  &  immediately  followed  by ENTER), any filtering is
              turned off, and all lines are displayed.  While filtering is  in
              effect,  an  ampersand  is  displayed  at  the  beginning of the
              prompt, as a reminder that some lines in the file may be hidden.
              Multiple  &  commands  may  be entered, in which case only lines
              which match all of the patterns will be displayed.

              Certain characters are special as in the / command:

              ^N or !
                     Display only lines which do NOT match the pattern.

              ^R     Don't interpret regular expression  metacharacters;  that
                     is, do a simple textual comparison.

所以在这里,你会这样做:&!natEnterthen &Ctrl+R192.168.1.1Enter,或者避免在example192.168.1.1内部匹配。192.168.1.123&Ctrl+R\<192\.168\.1\.1\>Enter

答案2

我深信较少的不允许仅显示带有192.168.1.1但不带有 的行nat

  • 您可以使用|(或)
  • ||&并且&&不存在
  • 您只能反转整个表达式的匹配(以!开头),但!之后并不特殊

另一种方法是使用sed较少的

sed -n -e '/nat/ d' -e '/192\.168\.1\.1/ p' FILE | less

相关内容