结合txt文件

时间:2018-03-15 14:33:06

标签: bash awk sed

在某些工作目录中,我有几个txt文件,其名称采用以下格式:

1.0bar_*.log
-5.0bar_*.log
-10.0bar_*.log
...
-50.0bar_*.log

最后一个文件名为:

Total_Contacts.log

我需要简单地循环workdir并将所有这些日志的内容合并到一个Final_summary.log中,保留上面显示的信息的顺序。

for res in ${output}/*.log; do
res_tit=$(basename "$res")
res_tit=${res_tit/.log/}
# print the name of the log firstly
printf "${res_tit}" >> ${output}/Summary_Final.log
# print the containt of the res file 
# some program
# introduce space
done

请注意,在Final_summary.log的开头,我需要添加一个特殊的字符串标记,以指示以下格式从中获取包含的文件的名称:

That info has been taken from the 01.0bar_*.log
# here is all of the containts of the 01.0bar_*.log

That info has been taken from the -05.0bar_*.log
# here is all of the containts of the -05.0bar_*.log


That info has been taken from the -10.0bar_*.log
# here is all of the containts of the -10.0bar_*.log

****************
And finally that is the summary from Summary_Final.log
# here is all of the containts of the Summary_Final.log

1 个答案:

答案 0 :(得分:0)

您可以使用sed将文件名作为第一列:

for FILE in `find . -type f -name "*.log"`
do
   cat $FILE | sed 's@^@\"'$FILE'\",@g' >> Total_Contacts.log
done

这样你就可以将所有文件连接在一起并将文件名显示为第一列,它会产生如下结果:

 01.0bar_*.log, "content of the file"
-05.0bar_*.log, "content of the file"
-10.0bar_*.log, "content of the file"