ChatGPT解决这个技术问题 Extra ChatGPT

连接多个文件,但包含文件名作为节标题

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

我目前拥有的:

file1.txt = bluemoongoodbeer

file2.txt = awesomepossum

file3.txt = hownowbrowncow

cat file1.txt file2.txt file3.txt

所需的输出:

file1

bluemoongoodbeer

file2

awesomepossum

file3

hownowbrowncow
我实际上正在使用 uuencode 和 uudecode 来做类似的事情,我不需要中间的文件可读,只需要结果并将它们再次通过管道传递给另一个命令
当您只想查看多个文件的内容时,batcat 的绝佳替代品

J
John Diamond

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

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


这也适用于 GNU tail(GNU Coreutils 的一部分)。
很棒的 -n +1 选项!另一种选择:head -n-0 file1 file2 file3
适用于 MacOS X 上的 BSD tail 和 GNU tail。您可以省略 -n+1 之间的空格,如 -n+1
tail -n +1 * 正是我正在寻找的,谢谢!
适用于 MacOsX 10.14.4 sudo ulimit -n 1024; find -f . -name "*.rb" | xargs tail -n+1 > ./source_ruby.txt
T
Theo

我用 grep 来做类似的事情:

grep "" *.txt

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


如果 *.txt 扩展为仅一个文件,则输出中断。在这方面,我建议grep '' /dev/null *.txt
+1 向我展示了 grep 的新用途。这完美地满足了我的需求。在我的例子中,每个文件只包含一行,所以它给了我一个格式整齐、易于解析的输出
grep 仅在有多个文件时打印文件头。如果要确保始终打印文件路径,请使用 -H。如果您不希望标题使用 -h。另请注意,它将为 STDIN 打印 (standard input)
您还可以使用 ag(银牌搜索器):默认情况下,ag . *.txt 为每个文件加上其名称的前缀,并在每一行加上其编号。
值得注意的是,将 -n 传递给 grep 也会产生行号,这使您可以编写简单的 linter 并精确定位,例如 emacs。
F
Flimm

这也应该可以解决问题:

$ 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 等布尔运算符组合搜索。 find -ls 也不错。


你能解释一下这个命令的作用吗?正是我需要的
这是 linux 的标准查找命令。它搜索当前目录中的所有文件,打印它们的名称,然后对每个文件进行分类。省略 -print 不会打印 cat 之前的文件名。
您还可以使用 -printf 自定义输出。例如:find *.conf -type f -printf '\n==> %p <==\n' -exec cat {} \; 匹配 tail -n +1 * 的输出
-printf 在 mac 上不起作用,除非您想要 brew install findutils 然后使用 gfind 而不是 find
如果您想要颜色,您可以使用 find 允许多个 -exec 的事实:find -name '*.conf' -exec printf '\n\e[33;1m%s\e[0m\n' {} \; -exec cat {} \;
C
Chris Eberle

这应该可以解决问题:

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

没用。我刚刚写了一些非常难看的 awk 代码: for i in $listofffiles do awk '{print FILENAME,$0,$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11}' $i >> concat.txt 完成
...想详细说明吗?这和 bash 代码一样简单。
@Nick:你的 awk 行甚至不应该工作,考虑到 $0 是整行,所以你实际上在那里有重复的列......
@Nick:否则很好的解决方案:)
@Chris:是的,但它比我希望的要丑得多。也许您的代码对我不起作用,因为我正在使用 >> 来捕获标准输出?
A
Asclepius

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

要连接到文件:

more *.txt > out.txt

要连接到终端:

more *.txt | cat

示例输出:

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

@Acumenus 对于我自己,我不得不使用:String="${String//$'\n'::::::::::::::$'\n'/|}" 然后:String="${String//::::::::::::::$'\n'/}" 最后:String="${String//$'\n'/|}" 来制作一个 YAD 数组:IFS='|' read -ra OLD_ARR <<< "$String"
@Acumenus 首先,我必须使用以下方法构建字符串字段:String=$(sudo -u "$SUDO_USER" ssh "$SUDO_USER"@"$TRG_HOST" \ "find /tmp/$SUDO_USER/scp.*/*.Header -type f \ -printf '%Ts\t%p\n' | sort -nr | cut -f2 | \ xargs more | cat | cut -d'|' -f2,3" \ )
@WinEunuuchs2Unix 我不关注。如果您愿意,可以在 gist.github.com 中更清楚地解释,并改为链接到它。
@Acumenus 更好的是,我会在完成后将脚本上传到 github。只是使用 sudo 在主机之间复制根文件,因为主机没有根帐户。注释中的代码是为以前的有效负载选择标头。大多数用户不会感兴趣的边缘事物。
这对我在 MacOS 上不起作用。我只得到文件内容。
K
Kevin
find . -type f -print0 | xargs -0 -I % sh -c 'echo %; cat %'

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


grep 结合起来也非常简单。与 bash 一起使用:find . -type f -print | grep PATTERN | xargs -n 1 -I {} -i bash -c 'echo ==== {} ====; cat {}; echo'
k
kvantour

缺少的 awk 解决方案是:

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

感谢您的精彩回答。你能解释一下表达式末尾的 1 的含义吗?
M
MorpheousMarty

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

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

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


这里要小心。 for i in * 不包括子目录。
c
ch3ll0v3k

我喜欢这个选项

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

k
keyser

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

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

d
drkvogel

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

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

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


s
sjas

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

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 *

为了突出显示文件名的颜色:find . -type f -name "*.txt" | xargs -I {} bash -c "echo $'\e[33;1m'{}$'\e[0m';cat {}"
T
Trey Brister

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

尝试这个

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

希望有帮助


更简洁的语法是 head -n -0 file.txt file2.txt file3.txt。适用于 GNU coreutils 中的 head
b
boroboris

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

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' - 将 ==> 替换为 new line + ### 并将 <== 替换为 ###

>> "files.txt" - 全部输出到文件


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

F
Fekete Sumér

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

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

说明:for 循环遍历 ls 命令产生的列表。 $ ls file{1..3}.txt 结果:file1.txt file2.txt file3.txt 每次迭代都会 echo $file 字符串,然后将其传送到 cut 命令中,我使用 . 作为字段分隔符,将 fileX.txt 分成两部分并打印出field1(field2是txt)其余的应该清楚
J
JustinKaisse

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

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


s
serenesat

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

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 ...

如果您想跟踪正在写入的文件,-f 很有用,但实际上不在 OP 要求的范围内。
此解决方案仅打印最后几行 - 而不是整个文件的内容。
I
Igor

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

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

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


但这并不能回答 OP 的问题。
J
John Ray

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