简体   繁体   English

tail -f file1 | perl -pe'$ _'> file2不向file2输出任何内容

[英]tail -f file1 | perl -pe '$_' > file2 does not output anything to file2

This command does not output anything to file2: 此命令不向file2输出任何内容:

#!/bin/bash
echo content > file1
tail -f file1 | perl -pe '$_' > file2

Whereas these commands work ok: 虽然这些命令可以正常工作:

tail -f file1 > file2
tail -f file1 | perl -pe '$_'
tail file1 | perl -pe '$_' > file2
tail -f /tmp/file1 | while read line; do echo $line | perl -pe '$_' > /tmp/file2 ; done

Anyone knows what's happening? 谁知道发生了什么?

perl has detected that stdout is not a terminal. perl检测到stdout不是终端。 For efficiency, it waits until it has a full block of data to write. 为了提高效率,它会一直等到要写入完整的数据块。 Since you don't provide more data, it won't write anything until tail exits and the program can finish. 由于您不提供更多数据,因此在tail退出并且程序可以完成之前不会写任何内容。

You can enable autoflushing with $|++ : 您可以使用$|++ 启用autoflushing

tail -f file1 | perl -pe '$|++; $_' > file2

The one with no output suffers from a combination of buffered output and non-termination. 没有输出的那个遭受缓冲输出和非终止的组合。 tail -f will never exit, waiting forever for file1 to grow. tail -f永远不会退出,永远等待file1增长。 However, the output of perl is (apparently) buffered, so it waits until it has some minimal amount of output (more than a single line containing "content") to actually write to file2 . 然而, perl的输出(显然)是缓冲的,所以它等到它有一些最小量的输出(超过一行包含“内容”)来实际写入file2 Until more output is generated, or perl exits, the output remains unflushed. 在生成更多输出或perl退出之前,输出仍保持未刷新状态。

Each of your working examples either terminates, flushing any buffered output at that time; 每个工作示例要么终止,要么刷新当时的任何缓冲输出; or its output is unbuffered, allowing it to appear as it is generated. 或者它的输出是无缓冲的,允许它在生成时显示。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打印文件1与文件2的差异,而不从文件2中删除任何内容 - print differences of file1 to file2 without deleting anything from file2 awk中的Vlookup:如何在输出末尾列出在file2中但不在file1中发生的所有事件? - Vlookup in awk: how to list anything occuring in file2 but not in file1 at the end of output? 比较File中的column1与File2中的column1,输出文件2中不存在的{Column1 File1} - Compare column1 in File with column1 in File2, output {Column1 File1} that does not exist in file 2 将文件 1 中的字符串与文件 2 中的字符串匹配 - Match string in file1 with string in file2 将File1与File2合并(继续从File1追加到File2,直到不再有行) - Merge File1 with File2 (keep appending from File1 to File2 until no more rows) 理解大括号对重定向的影响 ("> file1 >& file2" vs. "{ > file1; } >& file2") - Understanding the effect of curly braces with respect to redirections ("> file1 >& file2" vs. "{ > file1; } >& file2") bash 命令在文件 2 中的特定字符串/文本之后将文件内容从文件 1 复制到文件 2 - bash command to copy file contents from file1 to file2 after particular string/text in file2 sed用file1中的file3内容替换file2内容 - sed replace file2 content with file3 content in file1 如何将file1的每一列附加到file2的特定字段并创建一个新的输出文件? - How to append each column of file1 to a specific field of file2 and make a new output file? 比较文件1和文件2,但仅显示不在文件2中的新行 - Compare file1 and file2 but show only new lines which are not in file2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM