我想将多个文本文件连接到终端中的一个大文件中。我知道我可以使用 cat 命令来做到这一点。但是,我希望每个文件的文件名位于该文件的“数据转储”之前。有人知道怎么做吗?
我目前拥有的:
file1.txt = bluemoongoodbeer
file2.txt = awesomepossum
file3.txt = hownowbrowncow
cat file1.txt file2.txt file3.txt
所需的输出:
file1
bluemoongoodbeer
file2
awesomepossum
file3
hownowbrowncow
bat
是 cat
的绝佳替代品
正在寻找同样的东西,并发现这表明:
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
始终打印标题。
我用 grep 来做类似的事情:
grep "" *.txt
它不会给你一个“标题”,而是在每一行前面加上文件名。
*.txt
扩展为仅一个文件,则输出中断。在这方面,我建议grep '' /dev/null *.txt
grep
仅在有多个文件时打印文件头。如果要确保始终打印文件路径,请使用 -H
。如果您不希望标题使用 -h
。另请注意,它将为 STDIN 打印 (standard input)
。
ag
(银牌搜索器):默认情况下,ag . *.txt
为每个文件加上其名称的前缀,并在每一行加上其编号。
-n
传递给 grep 也会产生行号,这使您可以编写简单的 linter 并精确定位,例如 emacs。
这也应该可以解决问题:
$ 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
也不错。
-print
不会打印 cat
之前的文件名。
-printf
自定义输出。例如:find *.conf -type f -printf '\n==> %p <==\n' -exec cat {} \;
匹配 tail -n +1 *
的输出
brew install findutils
然后使用 gfind
而不是 find
。
find
允许多个 -exec
的事实:find -name '*.conf' -exec printf '\n\e[33;1m%s\e[0m\n' {} \; -exec cat {} \;
这应该可以解决问题:
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
$0
是整行,所以你实际上在那里有重复的列......
当有多个输入文件时,more
命令将它们连接起来,并将每个文件名作为标题包含在内。
要连接到文件:
more *.txt > out.txt
要连接到终端:
more *.txt | cat
示例输出:
::::::::::::::
file1.txt
::::::::::::::
This is
my first file.
::::::::::::::
file2.txt
::::::::::::::
And this is my
second file.
String="${String//$'\n'::::::::::::::$'\n'/|}"
然后:String="${String//::::::::::::::$'\n'/}"
最后:String="${String//$'\n'/|}"
来制作一个 YAD 数组:IFS='|' read -ra OLD_ARR <<< "$String"
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" \ )
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'
缺少的 awk 解决方案是:
$ awk '(FNR==1){print ">> " FILENAME " <<"}1' *
1
的含义吗?
这就是我通常处理格式的方式:
for i in *; do echo "$i"; echo ; cat "$i"; echo ; done ;
我通常将 cat 输入到 grep 中以获取特定信息。
for i in *
不包括子目录。
我喜欢这个选项
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 循环,
ls -ltr | awk '{print $9}' | xargs head
如果文件都具有相同的名称或可以通过 find
匹配,您可以这样做(例如):
find . -name create.sh | xargs tail -n +1
查找、显示每个文件的路径并对其进行分类。
如果你喜欢颜色,试试这个:
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 {}"
这是一个非常简单的方法。你说你想 cat,这意味着你想查看整个文件。但是您还需要打印文件名。
尝试这个
head -n99999999 *
或 head -n99999999 file1.txt file2.txt file3.txt
希望有帮助
head -n -0 file.txt file2.txt file3.txt
。适用于 GNU coreutils 中的 head
如果你想用其他东西替换那些丑陋的 ==> <==
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"
- 全部输出到文件
find . -type f -exec cat {} \; -print
如果您希望结果与所需输出的格式相同,您可以尝试:
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)其余的应该清楚
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 等)。
很抱歉发布衍生评论;我没有足够的街头信誉来评论/添加到某人的评论中。
此方法将打印文件名,然后是文件内容:
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 要求的范围内。
为了解决这个任务,我通常使用以下命令:
$ cat file{1..3}.txt >> result.txt
如果文件数量很大,这是一种连接文件的非常方便的方法。
首先我创建了每个文件: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
不定期副业成功案例分享
-n +1
选项!另一种选择:head -n-0 file1 file2 file3
。-n
和+1
之间的空格,如-n+1
。tail -n +1 *
正是我正在寻找的,谢谢!sudo ulimit -n 1024; find -f . -name "*.rb" | xargs tail -n+1 > ./source_ruby.txt