我需要一个 shell 脚本来设计以来自三个文件的模式打印行。
file1.txt, file2.txt,file3.txt
我需要的输出是
line1 of file1.txt
line2 of file1.txt
line1 of file2.txt
line2 of file2.txt
line1 of file3.txt
line2 of file3.txt
line3 of file1.txt
line4 of file1.txt
line3 of file2.txt
line4 of file2.txt
line3 of file3.txt
line4 of file3.txt
...
我们如何在 shell 脚本中获取它? 它也应该只打印非空行。
最佳答案
Perl 来拯救:
perl -e 'open $FH[ @FH ], "<", $_ or die $! for @ARGV;
while (grep !eof $_, @FH) {
for my $fh (@FH) {
print scalar <$fh> for 1, 2;
}
}' -- file*.txt
它保持所有文件同时打开(@FH 数组包含文件句柄)。虽然至少有一个尚未结束,但它会从每个打印两行。
关于linux - bash脚本需要来自多个文件的行bash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38462426/