尾巴 -f | sed 到文件不起作用

Ili*_*lia 2 shell filtering sed pipe tail

I am having an issue with filtering a log file that is being written and writing the output to another file (if possible using tee, so I can see it working as it goes).

I can get it to output on stdout, but not write to a file, either using tee or >>. I can also get it to write to the file, but only if I drop the -f options from tail, which I need.

So, here is an overview of the commands:

  1. tail -f without writing to file: tail -f test.log | sed 's/a/b/' works
  2. tail writing to file: tail test.log | sed 's/a/b/' | tee -a a.txt works
  3. tail -f writing to file: tail -f test.log | sed 's/a/b/' | tee -a a.txt doesn't output on stdout nor writes to file.

I would like 3. to work.

Jam*_*own 5

这是sed缓冲。使用sed -u. man sed

-u, --unbuffered

          load  minimal amounts of data from the input files and flush the
          output buffers more often
Run Code Online (Sandbox Code Playgroud)

这是它的测试(创建文件foobar):

$ for i in {1..3} ; do echo a $i ; sleep 1; done >> foo &
[1] 12218
$ tail -f foo | sed -u 's/a/b/' | tee -a bar
b 1
b 2
b 3
Run Code Online (Sandbox Code Playgroud)

快速或增加{1..3}以适合您的技能。