【问题标题】:Merge many csv files with similar names [closed]合并许多具有相似名称的 csv 文件 [关闭]
【发布时间】:2018-07-26 09:33:14
【问题描述】:

我有许多特定格式的 csv 文件,例如 1file1.csv 2file1.csv 3file2.csv 4file2.csv 5file3.csv 6file3.csv 等等。

我想合并所有文件名末尾有相同数字的文件。示例 1file1.csv 和 2file1.csv 应该连接起来。同样,3file2.csv 和 4file2.csv 应该连接起来。

有没有 linux 命令可以做到这一点? 还是优化的python代码?

【问题讨论】:

  • 它可能可以在 bash 中完成,但是当涉及到字符串处理(名称末尾的相同数字)时,在 Python 中编写、测试和维护它会更容易。只是一个意见,所以只有一个评论。
  • @SergeBallesta 好的。那么你知道python中有什么包吗?或者一个示例代码就可以了。
  • 请避免"Give me the codez" 问题。而是显示您正在处理的脚本并说明问题所在。另见How much research effort is expected of Stack Overflow users?

标签: linux csv concatenation python-3.6


【解决方案1】:

bash 中的解决方案:

printf "%s\n" [0-9]*file[0-9]*.csv | sed 's/.*file//;' | sort -u | xargs -I{} -- sh -c 'cat [0-9]*file{} > output_file{}'

文件 1file1.csv 2file1.csv 将连接到 output_file1.csv
文件 3file2.csv 4file2.csv 将连接到 output_file2.csv 中

【讨论】:

  • 完美运行。谢谢!
猜你喜欢
  • 2016-02-22
  • 2021-09-24
  • 2018-09-24
  • 2022-09-30
  • 2019-07-13
  • 2013-10-10
  • 2020-10-25
  • 2021-06-03
  • 1970-01-01
相关资源
最近更新 更多