在bash中将csv的单列水平分割成BASH中的多个较小的csv文件 [英] Split single column of csv horizontally in bash into multiple smaller csv files in BASH

查看:224
本文介绍了在bash中将csv的单列水平分割成BASH中的多个较小的csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用bash,并且在csv中有一个 * (不是行) *没有headers- samplefile.csv

  111 
222
333
444
555
666
777
888

我想把这个分割成2个csv文件和单个列(如果奇数表示9行,则为5和4)包含数据的CSV文件



output1.csv行)

  111 
222
333
444



和output2.csv(1列4行)

  555 
666
777
888

Csplit不会创建csv文件,如下所示
将文件拆分为x个文件,其中文件名编号



有任何建议吗?


< awk 尝试 awk'{print $ 0> (outputi + 1.csv)}!(NR%4){i ++}'file



<演示:

  $ ls 
档案

$ cat档案
111
222
333
444
555
666
777
888

$ awk'{ print $ 0> (outputi + 1.csv)}!(NR%4){i ++}'文件

$ ls
文件output1.csv output2.csv
b $ b $ cat output1.csv
111
222
333
444

$ cat output2.csv
555
666
777
888

说明: / p>

这里的模运算符是关键,我们要在第四行之后拆分输入行:

  $ awk'(print NR%4,$ 0}'file 
1 111
2 222
3 333
0 444
1 555
2 666
3 777
0 888

!(NR%4)是速记符号,因此我们使用这个事实来增加文件计数器。 NR%4 == 0 为零,计算结果为false, NR%4 code> {i ++} 执行,所以我们负数。

  $ awk'{print NR%4,$ 0,outputi + 1.csv 4){i ++}'file 
1 111 output1.csv
2 222 output1.csv
3 333 output1.csv
0 444 output1.csv
1 555 output2 .csv
2 666 output2.csv
3 777 output2.csv
0 888 output2.csv


I am using bash and I have a single column *(not row)* in csv with no headers- samplefile.csv

111 
222 
333 
444 
555 
666 
777 
888

I am looking to split this into (say)2 csv files of 4 rows and a single column each in this case (if odd number say 9 rows then 5 and 4) csv files with data

output1.csv (1 column 4 rows)

111
222
333
444

and output2.csv (1 column and 4 rows)

555
666
777
888

Csplit does not create csv files as shown here split a file into x files where file names are numbered

Any suggestions?

解决方案

This is simple with awk try awk '{print $0 > ("output"i+1".csv")}!(NR%4){i++}' file.

Demo:

$ ls 
file

$ cat file 
111 
222 
333 
444 
555 
666 
777 
888

$ awk '{print $0 > ("output"i+1".csv")}!(NR%4){i++}' file

$ ls
file  output1.csv  output2.csv

$ cat output1.csv 
111 
222 
333 
444 

$ cat output2.csv 
555 
666 
777 
888

Explanation:

The modulus operator is key here, we want to split the input line after every fourth line:

$ awk '{print NR%4,$0}' file
1 111
2 222
3 333
0 444
1 555
2 666
3 777
0 888

The modulus (remainder) of four at every fourth is of course zero so we use this fact to increment the file counter.!(NR%4) is shorthand for NR%4==0 as zero evaluates as false and NR%4 is zero when we want the block {i++} to execute so we negative it.

$ awk '{print NR%4,$0,"output"i+1".csv"}!(NR%4){i++}' file
1 111 output1.csv
2 222 output1.csv
3 333 output1.csv
0 444 output1.csv
1 555 output2.csv
2 666 output2.csv
3 777 output2.csv
0 888 output2.csv

这篇关于在bash中将csv的单列水平分割成BASH中的多个较小的csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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