4

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.

Irfy
  • 215
  • 2
  • 7
  • Seems related: http://unix.stackexchange.com/questions/33018/have-bash-script-wait-for-status-message-before-continuing – slm Jun 25 '13 at 15:27
  • You would need a [seekable pipe](http://unix.stackexchange.com/questions/30885/is-it-possible-to-make-seek-operations-on-a-named-pipe-return-successful), but there's no such facility. – Gilles 'SO- stop being evil' Jun 25 '13 at 23:29

1 Answers1

1

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.

Irfy
  • 215
  • 2
  • 7