In Unix "less", can I jump to the next line that does NOT contain X?

21

3

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".

dehmann

Posted 2010-10-02T10:59:24.117

Reputation: 1 853

Answers

29

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.

Casual Coder

Posted 2010-10-02T10:59:24.117

Reputation: 3 614

1This is perfect. Couldn't have expected a better answer. – dehmann – 2010-10-03T05:30:37.847

4

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.

sleske

Posted 2010-10-02T10:59:24.117

Reputation: 19 887

1

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.

Wil

Posted 2010-10-02T10:59:24.117

Reputation: 111