I would like to concatenate a number of text files into one large file in terminal. I know I can do this using the cat command. However, I would like the filename of each file to precede the "data dump" for that file. Anyone know how to do this?
我想把一些文本文件连接到终端的一个大文件中。我知道我可以用cat命令。但是,我希望每个文件的文件名在该文件的“数据转储”之前。有人知道怎么做吗?
what I currently have:
我目前拥有的:
file1.txt = bluemoongoodbeer
file2.txt = awesomepossum
file3.txt = hownowbrowncow
cat file1.txt file2.txt file3.txt
desired output:
期望的输出:
file1
bluemoongoodbeer
file2
awesomepossum
file3
hownowbrowncow
16 个解决方案
#1
350
Was looking for the same thing, and found this to suggest:
我也在寻找同样的东西,发现这表明:
tail -n +1 file1.txt file2.txt file3.txt
Output:
输出:
==> file1.txt <==
<contents of file1.txt>
==> file2.txt <==
<contents of file2.txt>
==> file3.txt <==
<contents of file3.txt>
If there is only a single file then the header will not be printed. If using GNU utils, you can use -v
to always print a header.
如果只有一个文件,那么头文件将不会被打印。如果使用GNU utils,您可以使用-v来始终打印标题。
#2
142
I used grep for something similar:
我用grep做了类似的事情:
grep "" *.txt
It does not give you a 'header', but prefixes every line with the filename.
它没有给你一个“标题”,但是在每一行都加上文件名。
#3
74
This should do the trick as well:
这一招也应该奏效:
find . -type f -print -exec cat {} \;
Means:
意思是:
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
You further can combine searches trough boolean operators like -and
or -or
. find -ls
is nice, too.
您还可以通过布尔运算符(如-or -or)组合搜索。查找-ls也很好。
#4
19
This should do the trick:
这应该能达到目的:
for filename in file1.txt file2.txt file3.txt; do
echo "$filename"
cat "$filename"
done > output.txt
or to do this for all text files recursively:
或者递归地对所有文本文件执行此操作:
find . -type f -name '*.txt' -print | while read filename; do
echo "$filename"
cat "$filename"
done > output.txt
#5
14
find . -type f -print0 | xargs -0 -I % sh -c 'echo %; cat %'
This will print the full filename (including path), then the contents of the file. It is also very flexible, as you can use -name "expr" for the find command, and run as many commands as you like on the files.
这将打印完整的文件名(包括路径),然后是文件的内容。它也非常灵活,因为您可以对find命令使用-name“expr”,并在文件中运行任意多的命令。
#6
13
I had a series of files that ended in stats.txt that I wanted to concatenate with the filenames.
我有一系列以stats结尾的文件。我想要连接到文件名的txt。
I did the following and when there is more than one file, the "more" command includes the filename as a header.
我做了以下操作,当有多个文件时,“more”命令包括文件名作为标题。
more *stats.txt > stats.txt
or for a general case
或者一般情况下。
more FILES_TO_CONCAT > OUTPUT_FILE
#7
4
This is how I normally handle formatting like that:
我通常是这样处理格式的:
for i in *; do echo "$i"; echo ; cat "$i"; echo ; done ;
I generally pipe the cat into a grep for specific information.
我通常会把这只猫放进一个grep中以获取特定的信息。
#8
3
you can use this simple command instead of using a for loop,
您可以使用这个简单的命令而不是使用for循环,
ls -ltr | awk '{print $9}' | xargs head
#9
3
I like this option
我喜欢这个选项
for x in $(ls ./*.php); do echo $x; cat $x | grep -i 'menuItem'; done
Output looks like this:
输出是这样的:
./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
#10
2
Here is a really simple way. You said you want to cat, which implies you want to view the entire file. But you also need the filename printed.
这里有一个非常简单的方法。您说您想要cat,这意味着您想查看整个文件。但是您还需要打印文件名。
Try this
试试这个
head -n99999999 *
or head -n99999999 file1.txt file2.txt file3.txt
头-n99999999 *或头-n99999999 file1。txt file2。txt file3.txt
Hope that helps
希望这有助于
#11
2
If you like colors, try this:
如果你喜欢颜色,试试这个:
for i in *; do echo; echo $'\e[33;1m'$i$'\e[0m'; cat $i; done | less -R
or:
或者:
tail -n +1 * | grep -e $ -e '==.*'
or: (with package 'multitail' installed)
或:(已安装'multitail'软件包)
multitail *
#12
1
This method will print filename and then file contents:
此方法将打印文件名,然后文件内容:
tail -f file1.txt file2.txt
Output:
输出:
==> file1.txt <==
contents of file1.txt ...
contents of file1.txt ...
==> file2.txt <==
contents of file2.txt ...
contents of file2.txt ...
#13
0
find . -type f -exec cat {} \; -print
#14
0
If you want the result in the same format as your desired output you can try:
如果你想要结果的格式与你想要的输出相同,你可以尝试:
for file in `ls file{1..3}.txt`; \
do echo $file | cut -d '.' -f 1; \
cat $file ; done;
Result:
结果:
file1
bluemoongoodbeer
file2
awesomepossum
file3
hownowbrowncow
You can put echo -e
before and after the cut so you have the spacing between the lines as well:
你可以在切割前后加上echo -e,这样你就有了线条之间的间距:
$ for file in `ls file{1..3}.txt`; do echo $file | cut -d '.' -f 1; echo -e; cat $file; echo -e ; done;
Result:
结果:
file1
bluemoongoodbeer
file2
awesomepossum
file3
hownowbrowncow
#15
0
- AIX 7.1 ksh
- AIX 7.1 ksh
... glomming onto those who've already mentioned head works for some of us:
…对那些已经提到过head的人说:
$ r head
head file*.txt
==> file1.txt <==
xxx
111
==> file2.txt <==
yyy
222
nyuk nyuk nyuk
==> file3.txt <==
zzz
$
My need is to read the first line; as noted, if you want more than 10 lines, you'll have to add options (head -9999, etc).
我需要读第一行;如前所述,如果您想要超过10行,您必须添加选项(head -9999,等等)。
Sorry for posting a derivative comment; I don't have sufficient street cred to comment/add to someone's comment.
对不起,我发布了一个衍生评论;我没有足够的信誉来评论/添加到某人的评论中。
#16
0
For solving this tasks I usually use the following command:
为了完成这个任务,我通常使用以下命令:
$ cat file{1..3}.txt >> result.txt
It's a very convenient way to concatenate files if the number of files is quite large.
如果文件的数量很大,这是连接文件的一种非常方便的方式。
#1
350
Was looking for the same thing, and found this to suggest:
我也在寻找同样的东西,发现这表明:
tail -n +1 file1.txt file2.txt file3.txt
Output:
输出:
==> file1.txt <==
<contents of file1.txt>
==> file2.txt <==
<contents of file2.txt>
==> file3.txt <==
<contents of file3.txt>
If there is only a single file then the header will not be printed. If using GNU utils, you can use -v
to always print a header.
如果只有一个文件,那么头文件将不会被打印。如果使用GNU utils,您可以使用-v来始终打印标题。
#2
142
I used grep for something similar:
我用grep做了类似的事情:
grep "" *.txt
It does not give you a 'header', but prefixes every line with the filename.
它没有给你一个“标题”,但是在每一行都加上文件名。
#3
74
This should do the trick as well:
这一招也应该奏效:
find . -type f -print -exec cat {} \;
Means:
意思是:
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
You further can combine searches trough boolean operators like -and
or -or
. find -ls
is nice, too.
您还可以通过布尔运算符(如-or -or)组合搜索。查找-ls也很好。
#4
19
This should do the trick:
这应该能达到目的:
for filename in file1.txt file2.txt file3.txt; do
echo "$filename"
cat "$filename"
done > output.txt
or to do this for all text files recursively:
或者递归地对所有文本文件执行此操作:
find . -type f -name '*.txt' -print | while read filename; do
echo "$filename"
cat "$filename"
done > output.txt
#5
14
find . -type f -print0 | xargs -0 -I % sh -c 'echo %; cat %'
This will print the full filename (including path), then the contents of the file. It is also very flexible, as you can use -name "expr" for the find command, and run as many commands as you like on the files.
这将打印完整的文件名(包括路径),然后是文件的内容。它也非常灵活,因为您可以对find命令使用-name“expr”,并在文件中运行任意多的命令。
#6
13
I had a series of files that ended in stats.txt that I wanted to concatenate with the filenames.
我有一系列以stats结尾的文件。我想要连接到文件名的txt。
I did the following and when there is more than one file, the "more" command includes the filename as a header.
我做了以下操作,当有多个文件时,“more”命令包括文件名作为标题。
more *stats.txt > stats.txt
or for a general case
或者一般情况下。
more FILES_TO_CONCAT > OUTPUT_FILE
#7
4
This is how I normally handle formatting like that:
我通常是这样处理格式的:
for i in *; do echo "$i"; echo ; cat "$i"; echo ; done ;
I generally pipe the cat into a grep for specific information.
我通常会把这只猫放进一个grep中以获取特定的信息。
#8
3
you can use this simple command instead of using a for loop,
您可以使用这个简单的命令而不是使用for循环,
ls -ltr | awk '{print $9}' | xargs head
#9
3
I like this option
我喜欢这个选项
for x in $(ls ./*.php); do echo $x; cat $x | grep -i 'menuItem'; done
Output looks like this:
输出是这样的:
./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
#10
2
Here is a really simple way. You said you want to cat, which implies you want to view the entire file. But you also need the filename printed.
这里有一个非常简单的方法。您说您想要cat,这意味着您想查看整个文件。但是您还需要打印文件名。
Try this
试试这个
head -n99999999 *
or head -n99999999 file1.txt file2.txt file3.txt
头-n99999999 *或头-n99999999 file1。txt file2。txt file3.txt
Hope that helps
希望这有助于
#11
2
If you like colors, try this:
如果你喜欢颜色,试试这个:
for i in *; do echo; echo $'\e[33;1m'$i$'\e[0m'; cat $i; done | less -R
or:
或者:
tail -n +1 * | grep -e $ -e '==.*'
or: (with package 'multitail' installed)
或:(已安装'multitail'软件包)
multitail *
#12
1
This method will print filename and then file contents:
此方法将打印文件名,然后文件内容:
tail -f file1.txt file2.txt
Output:
输出:
==> file1.txt <==
contents of file1.txt ...
contents of file1.txt ...
==> file2.txt <==
contents of file2.txt ...
contents of file2.txt ...
#13
0
find . -type f -exec cat {} \; -print
#14
0
If you want the result in the same format as your desired output you can try:
如果你想要结果的格式与你想要的输出相同,你可以尝试:
for file in `ls file{1..3}.txt`; \
do echo $file | cut -d '.' -f 1; \
cat $file ; done;
Result:
结果:
file1
bluemoongoodbeer
file2
awesomepossum
file3
hownowbrowncow
You can put echo -e
before and after the cut so you have the spacing between the lines as well:
你可以在切割前后加上echo -e,这样你就有了线条之间的间距:
$ for file in `ls file{1..3}.txt`; do echo $file | cut -d '.' -f 1; echo -e; cat $file; echo -e ; done;
Result:
结果:
file1
bluemoongoodbeer
file2
awesomepossum
file3
hownowbrowncow
#15
0
- AIX 7.1 ksh
- AIX 7.1 ksh
... glomming onto those who've already mentioned head works for some of us:
…对那些已经提到过head的人说:
$ r head
head file*.txt
==> file1.txt <==
xxx
111
==> file2.txt <==
yyy
222
nyuk nyuk nyuk
==> file3.txt <==
zzz
$
My need is to read the first line; as noted, if you want more than 10 lines, you'll have to add options (head -9999, etc).
我需要读第一行;如前所述,如果您想要超过10行,您必须添加选项(head -9999,等等)。
Sorry for posting a derivative comment; I don't have sufficient street cred to comment/add to someone's comment.
对不起,我发布了一个衍生评论;我没有足够的信誉来评论/添加到某人的评论中。
#16
0
For solving this tasks I usually use the following command:
为了完成这个任务,我通常使用以下命令:
$ cat file{1..3}.txt >> result.txt
It's a very convenient way to concatenate files if the number of files is quite large.
如果文件的数量很大,这是连接文件的一种非常方便的方式。