24

In the Unix less tool, is it possible to jump to the next line that does not contain a certain expression?

I sometimes have to look through files that contain thousands of similar adjacent lines that, for example, all say "Computing something" and I'm interested in the next line after the current block, which does not contain the word "Computing".

3 Answers 3

36

Yes, this is non-match search functionality, for example:

less file.conf

then you type / and after that ! your last line should look like:

Non-match /

then type your pattern for example Non-match /^# to look for first line without beginning #

Instead of ! character you can also use Ctrl+N.

2
  • 2
    This is perfect. Couldn't have expected a better answer.
    – dehmann
    Oct 3, 2010 at 5:30
  • For the backward non-matching search type ?!pattern. To repeat the previous search type /! + Enter or ?! + Enter.
    – Stalinko
    Jul 6, 2021 at 13:14
5

As an addition to Casual Coder's answer:

You could also filter out the lines altogether:

<logfile grep -v mypattern |less 

The disadvantage is that you have to quit and restart grep to change the pattern; the advantage is that the lines you don't want to see are hidden, which makes it easier to spot interesting patterns in the lines that you are interested in.

2

Not only can you jump between lines that don't contain X, you can hide the lines that do contain X, using less's & regex filter command.

less file.conf

then type &!^Computingthen the enter key and all those line will be hidden from view.

You can still search within the remaining lines with /.

Once you find the line you want you can bring the others back if you need their context - type & then the enter key to remove the filter.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .