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

我目前拥有的:

file1.txt = bluemoongoodbeer

file2.txt = awesomepossum

file3.txt = hownowbrowncow

cat file1.txt file2.txt file3.txt

期望的输出:

file1

bluemoongoodbeer

file2

awesomepossum

file3

hownowbrowncow

这应该可以达到目的:

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

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

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始终打印头文件。

你可以使用这个简单的命令来代替for循环,

ls -ltr | awk '{print $9}' | xargs head

这里有一个非常简单的方法。您说您想cat,这意味着您想查看整个文件。但是你还需要打印文件名。

试试这个

Head -n99999999 *或Head -n99999999 file1.txt file2.txt file3.txt

希望这能有所帮助

find . -type f -print0 | xargs -0 -I % sh -c 'echo %; cat %'

这将打印完整的文件名(包括路径),然后是文件的内容。它也非常灵活,因为您可以为find命令使用name“expr”,并在文件上运行任意多的命令。

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

连接到一个文件:

more *.txt > out.txt

连接到终端:

more *.txt | cat

示例输出:

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

我用grep来做类似的事情:

grep "" *.txt

它不会给你一个“标题”,而是给每一行加一个文件名前缀。

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

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

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

这也可以做到:

$ find . -type f -print -exec cat {} \;
./file1.txt
Content of file1.txt
./file2.txt
Content of file2.txt

下面是对命令行参数的解释:

find    = linux `find` command finds filenames, see `man find` for more info
.       = in current directory
-type f = only files, not directories
-print  = show found file
-exec   = additionally execute another linux command
cat     = linux `cat` command, see `man cat`, displays file contents
{}      = placeholder for the currently found filename
\;      = tell `find` command that it ends now here

您还可以通过布尔运算符组合搜索,如-and或-or。找到-ls也很好。

这个方法将先打印文件名,再打印文件内容:

tail -f file1.txt file2.txt

输出:

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

==> file2.txt <==
contents of file2.txt ...
contents of file2.txt ...
find . -type f -exec cat {} \; -print

如果你想要的结果与你想要的输出格式相同,你可以尝试:

for file in `ls file{1..3}.txt`; \
do echo $file | cut -d '.' -f 1; \ 
cat $file  ; done;

结果:

file1
bluemoongoodbeer
file2
awesomepossum
file3
hownowbrowncow

你可以在剪切之前和之后加上echo -e,这样你就有了行与行之间的间距:

$ for file in `ls file{1..3}.txt`; do echo $file | cut -d '.' -f 1; echo -e; cat $file; echo -e  ; done;

结果:

file1

bluemoongoodbeer

file2

awesomepossum

file3

hownowbrowncow

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,等等)。

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

如果你喜欢颜色,试试这个:

for i in *; do echo; echo $'\e[33;1m'$i$'\e[0m'; cat $i; done | less -R

or:

tail -n +1 * | grep -e $ -e '==.*'

或者:(安装包'multitail')

multitail *

我喜欢这个选项

for x in $(ls ./*.php); do echo $x; cat $x | grep -i 'menuItem'; done

输出如下所示:

./debug-things.php
./Facebook.Pixel.Code.php
./footer.trusted.seller.items.php
./GoogleAnalytics.php
./JivositeCode.php
./Live-Messenger.php
./mPopex.php
./NOTIFICATIONS-box.php
./reviewPopUp_Frame.php
            $('#top-nav-scroller-pos-<?=$active**MenuItem**;?>').addClass('active');
            gotTo**MenuItem**();
./Reviews-Frames-PopUps.php
./social.media.login.btns.php
./social-side-bar.php
./staticWalletsAlerst.php
./tmp-fix.php
./top-nav-scroller.php
$active**MenuItem** = '0';
        $active**MenuItem** = '1';
        $active**MenuItem** = '2';
        $active**MenuItem** = '3';
./Waiting-Overlay.php
./Yandex.Metrika.php

为了解决这个问题,我通常使用以下命令:

$ cat file{1..3}.txt >> result.txt

如果文件数量很大,这是一种非常方便的连接文件的方法。

如果你想用其他东西替换那些丑陋的==> <==

tail -n +1 *.txt | sed -e 's/==>/\n###/g' -e 's/<==/###/g' >> "files.txt"

解释:

Tail -n +1 *.txt -输出带头文件夹中的所有文件

sed - e ' s / = = > / \ n # # # / g - e ' s / < = = / # # # / g - = = >替换为新行< = = + # # #和# # #

>> "files.txt" -输出所有文件

如果所有文件都有相同的名称或可以通过find进行匹配,您可以这样做(例如):

find . -name create.sh | xargs tail -n +1

要查找,显示每个文件的路径并对其进行cat。

缺失的awk解是:

$ awk '(FNR==1){print ">> " FILENAME " <<"}1' *

首先,我创建了每个文件:echo 'information' > bfile1 .txt为每个文件[123].txt。

然后我打印每个文件以确保信息是正确的: 尾文件? . txt

然后我做了这个:tail file?.txt >> Mainfile.txt。这就创建了Mainfile.txt来将每个文件中的信息存储到一个主文件中。

cat Mainfile.txt确认没有问题。

==> 文件1.txt <== 蓝月好啤酒

==> file2.txt <== awesomepossum

==> file3.txt <== hownowbrowncow

我做了一个组合:

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

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