2009-12-01 212 views
9

我有这个FILE.CSV:基于bash.how中的字符串分隔符分割文件?

coordinate1,coordinate2,value1 
11111,a1,65 
11111,a2,32 
22222,b1,39 
22222,b3,55 
33333,c5,12 
33333,c9,16 
coordinate1,coordinate2,value2 
54656,a1,65 
21342,a2,32 
23543,b1,39 
123123,b3,55 
568568,c5,12 
568568,c9,16 
123123,b3,55 
568568,c5,12 
568568,c9,16 
coordinate1,coordinate2,value3 
23543,b1,39 
123123,b3,55 
568568,c5,12 
568568,c9,16 
123123,b3,55 
23543,b1,39 
123123,b3,55 
568568,c5,12 
568568,c9,16 
123123,b3,55 
11111,a1,65 
11111,a2,32 
22222,b1,39 
22222,b3,55 
33333,c5,12 
33333,c9,16 

现在,我想这个文件在3个文件分割,每个人都只能丝毫数据

Es: 1° file 
    coordinate1,coordinate2,value1 
    11111,a1,65 
    11111,a2,32 
    22222,b1,39 
    22222,b3,55 
    33333,c5,12 
    33333,c9,16 

Es: 2° file 
    coordinate1,coordinate2,value2 
    54656,a1,65 
    21342,a2,32 
    23543,b1,39 
    123123,b3,55 
    568568,c5,12 
    568568,c9,16 
    123123,b3,55 
    568568,c5,12 
    568568,c9,16 

回答

16

this forum公然被盗的集团:

awk '/YOUR_TEXT_HERE/{n++}{print >"out" n ".txt" }' final.txt 

应该做的伎俩(当然,取代YOUR_TEXT_HERE)。

与条件取代它,并与a.txt输入文件的输出发送到#file.txt

 
$ awk '/coordinate1,coordinate2,value?/{n++}{print > n "file.txt" }' a.txt 
$ ls 
1file.txt 2file.txt 3file.txt a.txt 
$ cat 1file.txt 
coordinate1,coordinate2,value1 
11111,a1,65 
11111,a2,32 
22222,b1,39 
22222,b3,55 
33333,c5,12 
33333,c9,16 
$ cat 2file.txt 
coordinate1,coordinate2,value2 
54656,a1,65 
21342,a2,32 
23543,b1,39 
123123,b3,55 
568568,c5,12 
568568,c9,16 
123123,b3,55 
568568,c5,12 
568568,c9,16 
$ cat 3file.txt 
coordinate1,coordinate2,value3 
23543,b1,39 
123123,b3,55 
568568,c5,12 
568568,c9,16 
123123,b3,55 
23543,b1,39 
123123,b3,55 
568568,c5,12 
568568,c9,16 
123123,b3,55 
11111,a1,65 
11111,a2,32 
22222,b1,39 
22222,b3,55 
33333,c5,12 
33333,c9,16 
+0

我试过这个例子。我得到以下错误:awk:源代码行1的语法错误 上下文是 \t/coordinate1,coordinate2,value?/ {n ++} {print> n >>>“file.txt”<<< – 2011-06-04 03:52:21

+0

@RaffiKhatchadourian哪个版本awk你用过吗? – Armali 2013-06-10 07:29:25

+0

@Armali awk version 20070501 – 2013-06-10 16:34:41

1

此不同引述对方的回答的版本也可以与Windows CMD:

awk "/coordinate1,coordinate2,value?/{n++}{print>n\"file.txt\"}" a.txt 
3

你可以使用csplit:

csplit file.txt /^c.*/ {*} 

This synta x在cygwin上工作,但没有在其他地方尝试过。