首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >合并txt文件

合并txt文件
EN

Stack Overflow用户
提问于 2018-03-15 22:33:06
回答 1查看 69关注 0票数 0

在一些work-dir中,我有几个txt文件,它们的名称采用以下格式:

代码语言:javascript
运行
复制
1.0bar_*.log
-5.0bar_*.log
-10.0bar_*.log
...
-50.0bar_*.log

最后是一个文件,名称为:

代码语言:javascript
运行
复制
Total_Contacts.log

我只需要循环工作目录,并将所有这些日志的容器合并到一个Final_summary.log中,从而保持上面显示的信息的顺序。

代码语言:javascript
运行
复制
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的开头,我需要添加一个特殊的字符串标记,以便以以下格式指示从中获取容器的文件的名称:

代码语言:javascript
运行
复制
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
EN

回答 1

Stack Overflow用户

发布于 2018-03-19 12:03:48

可以使用sed将文件名包括在第一列中:

代码语言:javascript
运行
复制
for FILE in `find . -type f -name "*.log"`
do
   cat $FILE | sed 's@^@\"'$FILE'\",@g' >> Total_Contacts.log
done

这样,您可以将所有文件连接在一起,并将文件名显示为第一列,结果如下所示:

代码语言:javascript
运行
复制
 01.0bar_*.log, "content of the file"
-05.0bar_*.log, "content of the file"
-10.0bar_*.log, "content of the file"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49302190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档