How to follow and seek in a file being piped through a filter, in less

lesssedtail

I basically wish to seek, search and follow a growing log file after passing it through a sed filter. I figured I need to follow a file with tail -f pipe it to sed and then pipe that to less. For testing, I first tried combining tail -f and less +F to no avail:

  • tail -f file | less +F — less doesn't run before one presses Ctrl+C to stop tail's following. less options -B -b 1 do not help.
  • less +F -f <(tail -f file) exhibits the same behavior.

If anyone knows a better/simpler solution than what I figured out in the end, I'd appreciate it.

Best Answer

Since piping directly didn't work, I tried connecting tail -f, sed and less +F via a temporary file. Ended up with the following

function lessx {
    [ -f "$1" ] || { echo "First argument must be a file"; return 1; }
    local tmpfile=$(mktemp /tmp/"$(basename "$1.XXX")")
    [ -f "$tmpfile" ] || { echo '`mktemp` failed'; return 1; }
    tail -f "$1" | stdbuf -i0 -o0 sed 's:\\\?\\n:\n:g' > "$tmpfile" &
    less +F "$tmpfile"
    kill %% # kill the tail pipeline, after less exits
    rm "$tmpfile"
}

which does the job, though it's more complicated than I'd like. Note that my SunOS tail doesn't understand --pid, so I manually kill the tail pipeline.

Related Question