linux - bash脚本需要来自多个文件的行bash

标签 linux bash shell awk scripting

我需要一个 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/

相关文章:

linux - 显示大于0的文件内容

c - "compiler helps implement the C standard?"是什么意思

bash - 从 shell 中删除文本文件的第一个字符

bash - 向上舍入 bc 比例而不是用 scale 或 printf 向下舍入

bash - Shell 脚本将所有输出转储到一个文件

python - 使用 Telegram - API 或 CLI 发送消息?

java - Bonjour - 适用于 Linux 的 DNS_SD.jar 库?

linux - linux 中 Java 运行时环境的内存不足

c - 如何从C程序执行命令

linux - 将文件内容回显到电子邮件主题中