我想将多个文本文件连接到终端中的一个大文件。我知道我可以使用cat命令来做到这一点。但是,我希望每个文件的文件名在该文件的“数据转储”之前。有人知道怎么做吗?

我目前拥有的:

file1.txt = bluemoongoodbeer

file2.txt = awesomepossum

file3.txt = hownowbrowncow

cat file1.txt file2.txt file3.txt

期望的输出:

file1

bluemoongoodbeer

file2

awesomepossum

file3

hownowbrowncow

当前回答

这是我通常如何处理这样的格式:

for i in *; do echo "$i"; echo ; cat "$i"; echo ; done ;

我通常将cat管道到grep中以获取特定的信息。

其他回答

这应该可以达到目的:

for filename in file1.txt file2.txt file3.txt; do
    echo "$filename"
    cat "$filename"
done > output.txt

或者递归地对所有文本文件执行此操作:

find . -type f -name '*.txt' -print | while read filename; do
    echo "$filename"
    cat "$filename"
done > output.txt

X 7.1泡芙

... 对我们中的一些人来说,抓住那些已经提到的头脑的人是有用的:

$ r head
head file*.txt
==> file1.txt <==
xxx
111

==> file2.txt <==
yyy
222
nyuk nyuk nyuk

==> file3.txt <==
zzz
$

我需要读第一行;如上所述,如果你想要超过10行,你必须添加选项(head -9999,等等)。

抱歉,我发表了一个衍生的评论;我没有足够的街头信誉来评论别人的评论。

也在寻找同样的东西,发现这表明

tail -n +1 file1.txt file2.txt file3.txt

输出:

==> file1.txt <==
<contents of file1.txt>

==> file2.txt <==
<contents of file2.txt>

==> file3.txt <==
<contents of file3.txt>

如果只有一个文件,则不会打印头文件。如果使用GNU utils,您可以使用-v始终打印头文件。

当有多个输入文件时,more命令将它们连接起来,并将每个文件名作为头文件。

连接到一个文件:

more *.txt > out.txt

连接到终端:

more *.txt | cat

示例输出:

::::::::::::::
file1.txt
::::::::::::::
This is
my first file.
::::::::::::::
file2.txt
::::::::::::::
And this is my
second file.

我做了一个组合:

Cat /sharedpath/{unique1,unique2,unique3}/filename >新文件

and

Tail -n +1 file1 file2

到这个:

Tail -n +1 /sharedpath/{folder1,folder2,…,folder_n}/文件。扩展名| cat > /sharedpath/newfile

结果是一个newfile,其中包含{}括号中每个子文件夹(unique1,unique2..)的内容,以子文件夹名称分隔。

注意unique1 = folder1

在我的例子中是文件。扩展名在所有子文件夹中具有相同的名称。