0

I'm trying to check new lines in a file using tail, then prepend each new line with something using sed, and print the output to the standard out.

The problem is, after adding a new line to the file, that line is not printed, but a previous one instead.

I am a Linux noob. So far I tried to pipe the output of sed to another program, like echo, printf, cat, more, less, but none of these worked.

I also tried adding "p" as a pattern flag at the end of the sed string, but that causes to print each string twice.

tail -F /usr/local/logs/myfile.log | sed -e "s/^/[hostname:$HOSTNAME] 
[file:myfile.log] /" 

I open another terminal and enter:

echo "test1" >> /usr/local/logs/myfile.log

(nothing happens, I expected message with "test1" to be printed)

echo "test2" >> /usr/local/logs/myfile.log

(prints [hostname:5b1dc0d27a45] [file:myfile.log] test1, I expect "test2")

If I only use tail, without sed, each new line is displayed properly - but of course not prepended with the stuff I wanted.

grooby666
  • 11
  • 3
  • What is your operation precisely, at "open another terminal and enter:"? What I actually want to ask is, what is the timeline and location of your `tail | sed` command, `echo >>` command and printed result? – Geno Chen Feb 01 '19 at 12:22
  • It works for me (when I use `\n` instead of a newline in the sed replacement). I just need to wait for a while. – choroba Feb 01 '19 at 12:28
  • @GenoChen Hi, wha I do is: 1) use this "tail -F ...." command as the command for my docker container 2) run the container 3) open another putty session to the machine where the docker is running 4) use docker exec to log into the running container 5) execute the "echo >>" commands. At this point i can still see the standard output from the container in the first window. – grooby666 Feb 01 '19 at 12:30
  • @choroba Can you share the sed string? I don't think I have any newline in the one that I posted. – grooby666 Feb 01 '19 at 12:32
  • I clearly see your sed expression has two lines, so there must be a newline. – choroba Feb 01 '19 at 14:12

1 Answers1

0

You use tail with follow mode. So the input for sed is not closed. For optimization, output is done in blocks (buffered) or at end. sed -u ... should help: It changes the output mode to unbuffered.

Wiimm
  • 2,971
  • 1
  • 15
  • 25
  • That worked, thank you! For me the problem is: my sed does not have the -u flag available. Would there be any way to workaround this? I cannot update any packages in the environment that I use. – grooby666 Feb 01 '19 at 13:22
  • maybe: `awk '{ gsub(/.../,"..."); fflush()}'` instead of `sed`. – Wiimm Feb 01 '19 at 14:02