0

我有从 400mb 到 1GB 的大文本文件。

它是一个制表符分隔的文件,日期是第二个字段。记录不按日期排序,可以按任何顺序排列。

记录的数量和期限因文件而异

文本文件中的每条记录都有一个格式为14/02/2012(ie. dd/mm/yyyy) 的日期字段。我想按日期拆分文本文件并保存为 Month.txt(例如2012Jan.txt)。

Jan.txt文件应仅包含以下期间1st Jan 2012的记录31st Jan 2012记录。

最好的方法是什么?有人可以推荐一个代码/编程工具来实现这一点。

谢谢

4

2 回答 2

0

在 UNIX 外壳中:

awk 'NR>1{split($2,date,"/");print>date[3]strftime("%b.txt",(date[2]-1)*31*24*60*60)}' large.txt

在 Windows CMD 外壳中:

awk "NR>1{split($2,date,\"/\");print>date[3]strftime(\"%b.txt\",(date[2]-1)*31*24*60*60)}" large.txt

在 Windows .BAT 文件中:

awk "NR>1{split($2,date,\"/\");print>date[3]strftime(\"%%b.txt\",(date[2]-1)*31*24*60*60)}" large.txt

(假设您的文件名为large.txt

于 2013-06-19T08:46:24.523 回答
0

in a Windows .BAT file, with header in each output file:

awk "NR==1{header=$0};NR>1{split($2,date,\"/\");file=date[3]strftime(\"%%b.txt\",(date[2]-1)*31*24*60*60);if(!wrote[file]++)print header>file;print>file}" %1

(here you pass the name of the input file as an argument to the .BAT call; if it's always the same file, you can change the %1 to that file name instead)

于 2013-06-21T12:02:50.867 回答