`Less` "Search Backward" Command Line Switch?
I Know Less +/Pattern Filename Will Open the File and Navigate to the First Occurrence of the Pattern. Is There a Way to Tell Less to Search from the End...
I know less +/pattern filename will open the file and navigate to the first occurrence of the pattern. Is there a way to tell less to search from the end backwards? Does it even accept more than one + switch?
Do not work:
less +?pattern filename
less +G +?pattern filename
2 Answers
The man page for less states the syntax is less +?pattern filename
?pattern
Search backward in the file for the N-th line containing the
pattern. The search starts at the last line displayed (but see
the -a and -j options, which change this).
Press n to continue to search for the pattern backwards. Press N to search for the pattern forwards.
Example
For testfile.txt of
apple
banana
carrot
apple
banana
carrot
apple
banana
carrot
less +?banana testfile.txt shows the following:
banana
carrot
In general it is better to escape ? and the pattern from the shell like this:
less '+?pattern' filename
In your case you might not get what you expect when e.g. there is a file matching +?pattern or you have set bash option nullglob (use shopt nullglob to check it).
In doubt, you can use echo to look what your shell changed:
echo less +?pattern filename