awk CSV拆分与标头Windows [英] awk CSV Split with headers Windows
问题描述
Ok我有一个csv文件,我需要根据列值拆分,这是罚款,但我不能得到打印在每个文件的头。
目前我使用:
awkFS = \,\{output = $ 3 \.csv \ ; print $ 0> output}test.csv
根据第3列分割文件文件,但我不知道如何添加标题到每个文件。
我搜索了高&低但找不到可在一个班轮中工作的解决方案...
UPDATE
到目前为止,我们有一个工作班子:
awk -F,NR == 1 {hdr = $ 0; next}! ($ 3 in files){files [$ 3] = 1; print hdr> $ 3\.csv\} {print> $ 3\.csv\}test.csv
或在test.awk中:
BEGIN {FS =,} NR == 1 {hdr = $ 0; next}!($ 3 in files){files [$ 3] = 1; print hdr> $ 3.csv} {print> $ 3。 csv}
要使用的命令:
awk -f test.awk test.csv
I真的很感谢在这里的帮助,我一直在尝试几个小时,有几件事情留下来工作。
1)空白行插入标题后
2 )对指定字段的数据进行排序
进一步下一行我想另外做一个行计数&
再次感谢。
div class =h2_lin>解决方案UPDATED#2
标题行后面的空行
UPDATED
请尝试:
On Unix / cygwin(我在cygwin上测试过):
awk -F,'NR == 1 {hdr = $ 0 ; next}!($ 3 in files){files [$ 3] = 1; print hdr\\\
> $ 3.csv} {print> $ 3.csv}'test.csv
或添加Kent的想法:
awk -F,'NR == 1 {hdr = $ 0; next} {out = $ 3.csv}!($ 3 in files){files [$ 3]; print hdr\\\
> out }在Windows cmd(未测试): awk -F,NR == 1 {hdr = $ 0; next}! ; print hdr \\\\
\> $ 3\.csv \} {print> $ 3\.csv\}test.csv
这会将 test.csv
中的标题行存储到 hdr
。对于下一行,它检查文件名值是否已经存在。如果不是,则将其名称存储在 files
哈希中,并打印标题行。
示例文件:
$ cat test.csv
A,B,C,D
1,2,a,3
4,5,b,4
输出
$ cat a.csv
A,B,C,D
1,2,a,3
$ cat b.csv
A,B ,C,D
4,5,b,4
ADDED
如果你想将 awk
脚本放入文件你可以试试,sorry)。
test.awk
BEGIN {FS = ,}
NR == 1 {hdr = $ 0; next}
!(files in file){files [$ 3] = 1; print hdr\\\
> $ 3.csv }
{print>$ 3.csv}
awk -f test.awk test.csv
Ok I have a csv file I need to split based on a column value which is fine, but I cannot get the headers to print in each file.
Currently I use:
awk "FS =\",\" {output=$3\".csv\"; print $0 > output}" test.csv
Which splits the files file based on column 3, but I don't know how to add the header to each file.
I've searched high & low but can't find a solution that will work in a one liner...
UPDATE
OK to date we have a working one liner:
awk -F, "NR==1{hdr=$0;next}!($3 in files){files[$3]=1;print hdr>$3\".csv\"}{print>$3\".csv\"}" test.csv
Or in test.awk:
BEGIN{FS=","} NR==1 {hdr=$0;next}!($3 in files) {files[$3]=1;print hdr>$3".csv"}{print>$3".csv"}
Command to run used:
awk -f test.awk test.csv
I really appreciate the help here, I've been trying for hours and have a few things left to work out.
1) Blank line inserted after header
2) Sort the data on specified fields
Further down the line I want to additionally do a row count & cut a reference number from another file is this possible with AWK or am I using the wrong tool for the job?
Thanks again.
解决方案 UPDATED#2
Blank line after header line
UPDATED
Try this:
On Unix/cygwin (I tested on cygwin):
awk -F, 'NR==1{hdr=$0;next}!($3 in files){files[$3]=1;print hdr"\n">$3".csv"}{print>$3".csv"}' test.csv
Or adding Kent's ideas:
awk -F, 'NR==1{hdr=$0;next}{out=$3".csv"}!($3 in files){files[$3];print hdr"\n">out}{print>out}' test.csv
On windows cmd (not tested):
awk -F, "NR==1{hdr=$0;next}!($3 in files){files[$3]=1;print hdr\"\n\">$3\".csv\"}{print>$3\".csv\"}" test.csv
This stores the header line in test.csv
to hdr
. For the next lines it checks if the file name value is already exists. If not then stores its name in the files
hash and prints the header line. And anyway it prints the whole line to the file.
Example file:
$ cat test.csv
A,B,C,D
1,2,a,3
4,5,b,4
Output
$ cat a.csv
A,B,C,D
1,2,a,3
$ cat b.csv
A,B,C,D
4,5,b,4
ADDED
If You would like to put the awk
script into a file You could try (I cannot test is, sorry).
test.awk
BEGIN{FS=","}
NR==1 {hdr=$0;next}
!($3 in files) {files[$3]=1;print hdr"\n">$3".csv"}
{print>"$3.csv"}
Then You may call it as
awk -f test.awk test.csv
这篇关于awk CSV拆分与标头Windows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!