Nic*_*ick 324 unix terminal header concatenation cat
我想将一些文本文件连接到终端中的一个大文件中.我知道我可以使用cat命令执行此操作.但是,我希望每个文件的文件名都在该文件的"数据转储"之前.有人知道怎么做吗?
我目前有什么:
file1.txt = bluemoongoodbeer
file2.txt = awesomepossum
file3.txt = hownowbrowncow
cat file1.txt file2.txt file3.txt
Run Code Online (Sandbox Code Playgroud)
期望的输出:
file1
bluemoongoodbeer
file2
awesomepossum
file3
hownowbrowncow
Run Code Online (Sandbox Code Playgroud)
DS.*_*DS. 477
正在寻找相同的东西,并发现这建议:
tail -n +1 file1.txt file2.txt file3.txt
Run Code Online (Sandbox Code Playgroud)
输出:
==> file1.txt <==
<contents of file1.txt>
==> file2.txt <==
<contents of file2.txt>
==> file3.txt <==
<contents of file3.txt>
Run Code Online (Sandbox Code Playgroud)
如果只有一个文件,则不会打印标题.如果使用GNU utils,您可以使用-v始终打印标题.
小智 174
我用grep做类似的事情:
grep "" *.txt
Run Code Online (Sandbox Code Playgroud)
它没有给你一个'标题',但每行都有文件名前缀.
Max*_*ted 97
这应该也可以解决问题:
find . -type f -print -exec cat {} \;
Run Code Online (Sandbox Code Playgroud)
手段:
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
Run Code Online (Sandbox Code Playgroud)
您还可以通过布尔运算符(如-and或)组合搜索-or.find -ls也很好.
Chr*_*rle 23
这应该做的伎俩:
for filename in file1.txt file2.txt file3.txt; do
echo "$filename"
cat "$filename"
done > output.txt
Run Code Online (Sandbox Code Playgroud)
或者以递归方式对所有文本文件执行此操作:
find . -type f -name '*.txt' -print | while read filename; do
echo "$filename"
cat "$filename"
done > output.txt
Run Code Online (Sandbox Code Playgroud)
小智 16
我有一系列以stats.txt结尾的文件,我希望与文件名连接.
我做了以下操作,当有多个文件时,"more"命令包含文件名作为标题.
more *stats.txt > stats.txt
Run Code Online (Sandbox Code Playgroud)
或者一般情况
more FILES_TO_CONCAT > OUTPUT_FILE
Run Code Online (Sandbox Code Playgroud)
kev*_*inf 15
find . -type f -print0 | xargs -0 -I % sh -c 'echo %; cat %'
Run Code Online (Sandbox Code Playgroud)
这将打印完整的文件名(包括路径),然后打印文件的内容.它也非常灵活,因为你可以使用-name"expr"作为find命令,并在文件上运行任意数量的命令.
缺少的 awk 解决方案是:
$ awk '(FNR==1){print ">> " FILENAME " <<"}1' *
Run Code Online (Sandbox Code Playgroud)
小智 5
这就是我通常处理格式的方式:
for i in *; do echo "$i"; echo ; cat "$i"; echo ; done ;
Run Code Online (Sandbox Code Playgroud)
我通常将 cat 输入 grep 以获取特定信息。
我喜欢这个选项
for x in $(ls ./*.php); do echo $x; cat $x | grep -i 'menuItem'; done
Run Code Online (Sandbox Code Playgroud)
输出看起来像这样:
./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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
168190 次 |
| 最近记录: |