按日期列值分割文件 [英] Split file by date column value

查看:167
本文介绍了按日期列值分割文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有89列的csv文件,它包含多个数据日期.日期列是第59列.日期格式为mm/dd/yy.我想实现的是用mmddyyyy.csv创建一个文件,因此每个文件将只包含一个数据日期.

I have a csv file that has 89 columns and it contains multiple dates of data. The date column is the 59th column. The date format is mm/dd/yy. What I would like to achieve is to create a file with mmddyyyy.csv, so each file will contain only one date of data.

到目前为止,我的方法是

So far my approach is

  1. 从文件中获取所有不同的日期
  2. 对于每个不同的日期

  1. Get all distinct date from files
  2. For each distinct date

grep [date value] file > mmddyyyy.csv

上述解决方案的唯一缺点是,如果[date value]存在于行中的任何位置,它将被拾取,并且我可能最终会在两个或多个文件中得到一条记录.

The only drawback of the above solution is that if [date value] exists anywhere in the line, it will be picked up and I possibly will end up with one record in two or more files.

我知道对于单个值(字符串/数字),awk可以像

I know for a single value (string/number) awk can do it straight forward like

awk -F"\t" '{print >> ($14".csv");}' $1

推荐答案

听起来您只需要:

awk -F, '{split($59,t,"/"); print > (t[1] t[2] "20" t[3] ".csv")}' file

,但没有示例输入和预期输出,这只是一个猜测.如果您不使用GNU awk,则可能需要在适当的时候添加close(),以避免同时打开太多文件.

but without sample input and expected output it's a guess. If you're not using GNU awk you might need to throw in a close() when appropriate to avoid having too many files open simultaneously.

这篇关于按日期列值分割文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆