简体   繁体   English

Linux-根据条件分割CSV

[英]Linux - Split CSV according to condition

I have a large CSV file with the following records: 我有一个大型CSV文件,其中包含以下记录:

60,1572236,3,58394247,\N,\N,\N,1925720,\N,2011-11-25 12:55:53,2011-11-25 12:55:53
61,2875109,4,58394209,1234,\N,4025175,\N,\N,2011-11-25 12:55:53,2011-11-25 12:55:53
62,2875109,4,58394209,\N,\N,\N,9860904,\N,2011-11-25 12:55:53,2011-11-25 12:55:53
63,2875109,5,58394209,8756,\N,\N,8615157,\N,2011-11-25 12:55:53,2011-11-25 12:55:53
64,2686696,3,58394211,\N,\N,59512,\N,\N,2011-11-25 12:55:53,2011-11-25 12:55:53
65,2686696,3,58394211,\N,\N,4025893,\N,\N,2011-11-25 12:55:53,2011-11-25 12:55:53

I want to split the file into 2 file, the condition for the split will be the value in the 5th column, one file will get the rows with no value in that column (\\N) and columns with value in that column will go to another file. 我想将文件拆分为2个文件,拆分的条件将是第5列中的值,一个文件将获得该列(\\ N)中没有值的行,并且该列中具有值的列将转到另一个文件。

in the example above: rows 2,4 will go to one file and the rest of the rows will go to another file. 在上面的示例中:第2,4行将转到一个文件,其余的行将转到另一个文件。

any ideas how to do this in linux? 任何想法如何在Linux中做到这一点?

You can use awk : 您可以使用awk

$ awk -F, '$5=="\\N"{print >"file1.csv"; next}; {print >"file2.csv"}' data.csv
  • file1.csv will contains \\N file1.csv将包含\\N
  • file2.csv will contains others file2.csv将包含其他文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM