View files without lines with certain string

The pattern match inversion option -v for grep is really helpful for this:

grep -v 'UFW BLOCK' /var/log/syslog

This will show you all lines not containing UFW BLOCK. As grep uses basic regular expressions by default, the inclusion of the brackets will make it search for any of the individual characters of 'UFW BLOCK' including the space. You'll probably end up with no output. If you need to ensure that there are brackets around the string, either escape them \[UFW BLOCK\], or use the -F option of grep to only include fixed strings (Thanks to Zanna and Steeldriver for the advice on this):

grep -Fv '[UFW BLOCK]' /var/log/syslog

You can make it easier to view by piping the output to a pager like less:

grep -v 'UFW BLOCK' /var/log/syslog | less

Or redirect the output to a file in your home directory for later viewing:

grep -v 'UFW BLOCK' /var/log/syslog > ~/filtered_syslog

You can also use sed's d command to delete the lines with the pattern from the stream:

sed '/\[UFW BLOCK\]/d' /var/log/syslog

We escape [] as normally they denote a character class, meaning "match anything inside here"


You can use any tool with editing capabilities. You've already been given solutions using grep and sed, here are a few other choices. All of these can easily be piped to less or more or anything else.

  1. Perl

    perl -ne 'print unless /\[UFW BLOCK\]/' /var/log/syslog
    

    Since this is Perl, TIMTOWDI!.

    perl -pe '$_="" if /\[UFW BLOCK\]/' /var/log/syslog
    perl -ne '/\[UFW BLOCK\]/ || print' /var/log/syslog
    perl -ne 'print if !/\[UFW BLOCK\]/' /var/log/syslog
    perl -ne '!/\[UFW BLOCK\]/ && print' /var/log/syslog
    perl -ne '/\[UFW BLOCK\]/ ? "" : print' /var/log/syslog
    
  2. awk

    awk '!/\[UFW BLOCK\]/' file