Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unix 在读取多个文件并写入一个文件后,包括文件名和路径';大';文件_Unix - Fatal编程技术网

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
命令没有给我文件的路径如果你想更灵活地输出,你可能想看看我的答案。这将只输出文件中第一行旁边的文件名,不适用于所有行。问题中未指定此选项:)这将仅输出文件中第一行旁边的文件名,而不是所有行。问题中未指定此选项:)