2

From a script, I want to invoke less on a file and have it print the output to the console, rather than a new screen. If the file is short enough to fit on one screen, I want to disable scrolling. If it's longer than one screen, I want to be able to scroll through it, but once I hit the bottom, I want less to return control to the console. Lines should also be chopped (it's okay to lose the end of the strings past the console window in this case).

I also would like it to highlight a certain pattern.

Here is what I'm using for arguments:

less -SFXE -p "ccc" fileToShow.txt

I use -S to chop long lines, -FX to detect if the file has less lines than the console and print all the text to the console without scrolling, and -E to quit less when I've reached the end of the file, for the cases when the text is longer than the console and I need scrolling.

These work well until I add the -p switch for highlighting matches.

Suppose fileToShow.txt contains this:

aaa
bbb
ccc
ddd
eee

Without the pattern switch, I get this:

[evan@localhost] $ less -SFXE fileToShow.txt
aaa
bbb
ccc
ddd
eee
[evan@localhost] $ 

When I add the pattern matching, less prints out the empty lines up to the console height (using tildes to show the empty lines).

[evan@localhost] $ less -SFXE -p "ccc" fileToShow.txt
ccc
ddd
eee
~
~
~
~
~
~
~
~
[evan@localhost] $ 

Is there any way to use the -p switch and not have it show those empty lines when using -F? (Note: the "ccc" line is highlighted as desired, I just have this unintended side effect.)

My shell is bash 3.2.25 and my less version is 436. OS is RHEL.

  • I suppose -p not only searches and highlights patterns, but also moves the cursor to the first match. I think that the cursor moving part is what causes less to show those empty lines. Is there a way to get the highlighting without moving the cursor? – tomocafe Aug 5 '15 at 22:20
  • 1
    The following could be encapsulated in a batch file: grep --color=always -E "^|$2" "$1"|less -SFXER, where $1 is the file name and $2 is the search pattern. Not exactly neat, but some sort of answer. Note that this would list all the lines, highlighting the pattern, whereas less -p omits lines ahead of the first match: to do this you would need to use sed. – AFH Aug 5 '15 at 22:57
  • Yes, that works perfectly for me. If you'd like to paste it to an answer, I'd be happy to accept it. – tomocafe Aug 5 '15 at 23:27
1

The following command could be encapsulated in a batch file:

 grep --color=always -E "^|$2" "$1"|less -SFXER

The parameter $1 is the file name and $2 is the search pattern. Points to note:

  • grep -E (or egrep) allows matching to more than one search pattern;
  • matching to ^ (start of line) ensures that every line is listed;
  • matching $2 causes the search string to be highlighted;
  • grep --color=always copies the highlighting escape sequences to the pipe;
  • less -R makes less reproduce the highlighting instead of showing the escape sequence.

It is not the neatest of solutions, but some sort of answer. Note that the command will list all the lines, highlighting the pattern, whereas less -p omits lines ahead of the first match: to reproduce this you would need to use sed.

0

There's another way, all in less. It involves a few steps, though.

  1. less fileToShow.txt
  2. Hit &ccc (assuming ccc is the pattern you're looking for). From the man page:

    &pattern: Display only lines which match the pattern; lines which do not match the pattern are not displayed. If pattern is empty (if you type & immediately followed by ENTER), any filtering is turned off, and all lines are displayed. While filtering is in effect, an ampersand is displayed at the beginning of the prompt, as a reminder that some lines in the file may be hidden. Certain characters are special as in the / command: ^N or ! Display only lines which do NOT match the pattern. ^R Don't interpret regular expression metacharacters; that is, do a simple textual comparison.

  3. Finally, press F to resume tailing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.