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

相關內容