Divide very large file into small ones following pattern

别说谁变了你拦得住时间么 提交于 2019-12-24 13:05:44

问题


I have been working on this problem with only little success so I am coming here to get some fresh advices.

I am trying to extract the data of every scan into separate files.

The problem is that after 3196 files created I receive the error message : awk “makes too many open files”.

I understand that I need to close the files created by awk but I don't know how to do that.

Text inputfile is looking like this (up to 80 000 Scan):

Scan    1
11111    111
22222    221
...
Scan    2
11122    111
11122    111
...
Scan    3
11522    141
19922    141
...

For now I have been doing :

awk '/.*Scan.*/{n++}{print >"filescan" n }' inputfile

Which gives me an incremented output file for every Scan and crash after 3196 files created..

cat filescan1
Scan    1
11111    111
22222    221
...

Any idea ?


回答1:


You need to close the output file as awk is keeping the file handle open.

awk '/.*Scan.*/{ 
  close(file);
  n++;
}
{ 
  file="filescan"n; 
  print >> file;
}' inputfile


来源:https://stackoverflow.com/questions/32228798/divide-very-large-file-into-small-ones-following-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!