Unix 在读取多个文件并写入一个文件后,包括文件名和路径';大';文件
在将多个文件合并为一个“大”文件后,如何包含文件名和路径 例如,我在目录Unix 在读取多个文件并写入一个文件后,包括文件名和路径';大';文件,unix,Unix,在将多个文件合并为一个“大”文件后,如何包含文件名和路径 例如,我在目录/temp/Errors/中有文件TestMessage1.txt到TestMessage5.txt。这些文件的内容是Hello,这是测试消息1到Hello,这是测试消息5 使用cat*>outputfile.txt 在输出文件中,我得到: Hello, this is test message 1 Hello, this is test message 2 Hello, this is test message 3 Hel
/temp/Errors/
中有文件TestMessage1.txt到TestMessage5.txt
。这些文件的内容是Hello,这是测试消息1到Hello,这是测试消息5
使用cat*>outputfile.txt
在输出文件中,我得到:
Hello, this is test message 1
Hello, this is test message 2
Hello, this is test message 3
Hello, this is test message 4
Hello, this is test message 5
如何使输出文件看起来像:
/temp/Errors/TestMessage1 : Hello, this is test message 1
/temp/Errors/TestMessage2 : Hello, this is test message 2
/temp/Errors/TestMessage3 : Hello, this is test message 3
/temp/Errors/TestMessage4 : Hello, this is test message 4
/temp/Errors/TestMessage5 : Hello, this is test message 5
提前感谢使用grep
:
grep "" * > outputfile.txt
如果需要文件的完整路径,请使用:
grep "" /temp/Errors/* > outputfile.txt
如果要,
而不是:
,请将管道连接到sed
:
grep "" /temp/Errors/* | sed 's/:/,/'> outputfile.txt
使用grep
:
grep "" * > outputfile.txt
如果需要文件的完整路径,请使用:
grep "" /temp/Errors/* > outputfile.txt
如果要,
而不是:
,请将管道连接到sed
:
grep "" /temp/Errors/* | sed 's/:/,/'> outputfile.txt
这可以通过(bash)脚本完成:
用于i in*;做
读行时;做
完整=$PWD/$i
echo“$full,$line”
完成<$i
完成
此解决方案的优点是比grep更灵活
(已编辑用于多行,逗号代替分号和完整路径)可使用(bash)脚本完成:
用于i in*;做
读行时;做
完整=$PWD/$i
echo“$full,$line”
完成<$i
完成
此解决方案的优点是比grep更灵活
(已编辑对于多行,逗号代替分号和完整路径)谢谢。。给出了我要查找的内容。这个grep命令将匹配文件中的所有内容,并将其与文件名一起打印出来。试试看。我注意到一个
:
用于将文件名与文件内容分开。是否可以改用,
。这个grep
命令没有给我文件的路径。如果您想要更灵活的输出,您可能想看看我的答案。谢谢。。给出了我要查找的内容。这个grep命令将匹配文件中的所有内容,并将其与文件名一起打印出来。试试看。我注意到一个:
用于将文件名与文件内容分开。是否可以改用,
。这个grep
命令没有给我文件的路径如果你想更灵活地输出,你可能想看看我的答案。这将只输出文件中第一行旁边的文件名,不适用于所有行。问题中未指定此选项:)这将仅输出文件中第一行旁边的文件名,而不是所有行。问题中未指定此选项:)