0

When I open a log file in vi or view and I need to search for a word, the first thing I do is to press G to get to the bottom. Then I'll start a search using a / followed by whatever word I'm looking for. This usually works for me.

Today someone mentioned I should use ? to start my search since I'm starting at the bottom of the file and this will cause it to search from the bottom up.

My question is should I have been using the ? all along, since these are log files and I will usually want to find the most recent occurrence of the "word"?

Also where does the search start when using the /, from the top or the current line the cursor is on?

guntbert
  • 1,245
  • 1
  • 13
  • 27
datacon
  • 3
  • 2

1 Answers1

0

Yes, / will search forward and ? will search backward.

Both will (by default) wrap around when they get to the bottom/top of the file.

To find the most recent occurrence of the search pattern in a log file, you typically want to use ?, to search backward, wrapping at the top and starting from the bottom of the file.

Unless you have a match on the very last line, using G is not necessary. In fact, it might make you miss a match, having the backward search start at the beginning of the last line of the file, rather than also search for the match on that last line.

filbranden
  • 28,785
  • 3
  • 26
  • 71