如何获取所有压缩文件的前 10 行?

How can I get the 10 first lines of all my compressed files?

我有一堆 M 文件,我想从中提取前 N 行(来自 each)。我的文件在 BZ2 中压缩。否则,做head -10 *就够了。

例如:假设我想从我的所有文件中提取前两行(A.txt、B.txt、C.txt)。

A.txt:

1A
2A
3A
4A

B.txt:

1B

C.txt:

1C
2C

预期的结果应该是这样的(即包含这些行,而不是强制按顺序排列):

1A
2A
1B
1C
2C

我尝试了以下方法:

有人知道吗?

使用 for 循环:

for i in *; do bzcat "$i" | head -n10; done

find:

find -type f -exec bash -c 'bzcat  | head -n10' _ {} \;

使用xargs

find . -name "*" | xargs -I {} sh -c "bzcat {} | head -n10"

xargs -I 替换-str :

Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character.