Question

In an earlier question (Use awk to output section of file forward and back from target regex) I developed the following statement that pipes output from gawk into less.

gawk -v RS="${recordSetDelimiter}" "BEGIN{IGNORECASE=$ignoreCase} /${searchTerm}/{print FILENAME ORS \$0 ORS \"<<\"}" "${directory}"/*.idx | less -I -p "${searchTermLess}"

However, I want the ability to avoid less if the input is empty. I thought about variables or temp text files, and I guess this could work, but the input might be big and I am not sure how that would affect handling. (Since what I love about this command is just how quick it is.)

Is there a "best way" to handle this? How to also handle input that is just empty lines?

Was it helpful?

Solution

Use the -F option to less.

gawk -v RS="${recordSetDelimiter}" "BEGIN{IGNORECASE=$ignoreCase} /${searchTerm}/{print FILENAME ORS \$0 ORS \"<<\"}" "${directory}"/*.idx |
    less -F -I -p "${searchTermLess}"

This makes it exit immediately if the output would fit on one screen, which is trivially true if there's no output.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top