我想将多个文本文件连接到终端中的一个大文件。我知道我可以使用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; 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

这应该可以达到目的:

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

首先,我创建了每个文件: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

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

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

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