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:
tail -f without writing to file: tail -f test.log | sed 's/a/b/' workstail writing to file: tail test.log | sed 's/a/b/' | tee -a a.txt works 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.
这是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)
这是它的测试(创建文件foo和bar):
$ 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}以适合您的技能。