424

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

我目前拥有的:

file1.txt = bluemoongoodbeer

file2.txt = awesomepossum

file3.txt = hownowbrowncow

cat file1.txt file2.txt file3.txt

所需的输出:

file1

bluemoongoodbeer

file2

awesomepossum

file3

hownowbrowncow
4

20 回答 20

647

正在寻找同样的东西,并发现这表明:

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来始终打印标题。

于 2011-10-19T04:19:28.533 回答
220

我用 grep 来做类似的事情:

grep "" *.txt

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

于 2012-08-30T10:00:14.670 回答
121

这也应该可以解决问题:

find . -type f -print -exec cat {} \;

方法:

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之类的布尔运算符组合搜索-orfind -ls也不错。

于 2014-01-30T15:10:36.657 回答
27

这应该可以解决问题:

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
于 2011-05-06T22:08:32.570 回答
24

当有多个输入文件时,该more命令将它们连接起来,并且还包括每个文件名作为标题。

要连接到文件:

more *.txt > out.txt

要连接到终端:

more *.txt | cat

示例输出:

::::::::::::::
file1.txt
::::::::::::::
This is
my first file.
::::::::::::::
file2.txt
::::::::::::::
And this is my
second file.
于 2012-06-12T20:31:05.900 回答
15
find . -type f -print0 | xargs -0 -I % sh -c 'echo %; cat %'

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

于 2012-05-11T14:59:06.047 回答
7

缺少的 awk 解决方案是:

$ awk '(FNR==1){print ">> " FILENAME " <<"}1' *
于 2019-05-24T09:55:51.517 回答
5

这就是我通常处理格式的方式:

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

我通常将 cat 输入到 grep 中以获取特定信息。

于 2013-01-16T12:19:09.860 回答
5

我喜欢这个选项

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
于 2017-08-02T15:50:37.687 回答
4

您可以使用这个简单的命令而不是使用 for 循环,

ls -ltr | awk '{print $9}' | xargs head
于 2012-01-13T15:18:43.893 回答
4

如果文件都具有相同的名称或可以匹配find,你可以这样做(例如):

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

查找、显示每个文件的路径并对其进行分类。

于 2019-04-16T11:05:57.783 回答
3

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

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

或者:

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

或:(安装了包'multitail')

multitail *
于 2016-08-26T20:32:05.823 回答
2

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

尝试这个

head -n99999999 *或者head -n99999999 file1.txt file2.txt file3.txt

希望有帮助

于 2012-03-04T18:40:43.123 回答
2

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

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"- 全部输出到文件

于 2018-10-04T16:51:19.437 回答
1
find . -type f -exec cat {} \; -print
于 2015-12-17T03:50:39.893 回答
0

如果您希望结果与所需输出的格式相同,您可以尝试:

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
于 2016-02-01T12:50:45.760 回答
0
  • AIX 7.1 ksh

......对那些已经提到我们中的一些人的头部作品的人感到困惑:

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

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

==> file3.txt <==
zzz
$

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

很抱歉发布衍生评论;我没有足够的街头信誉来评论/添加到某人的评论中。

于 2016-03-21T15:46:30.930 回答
-1

此方法将打印文件名,然后是文件内容:

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 ...
于 2015-04-13T09:57:26.157 回答
-2

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

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

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

于 2018-04-15T09:08:45.117 回答
-3

首先我创建了每个文件:echo 'information' > file1.txt for each file[123].txt。

然后我打印了每个文件以确保信息正确:tail file?.txt

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

cat Mainfile.txt 确认没问题。

==> file1.txt <== bluemoongoodbeer

==> file2.txt <== awesomepossum

==> file3.txt <== hownowbrowncow

于 2019-09-17T05:52:27.173 回答