在bash.how中基于字符串分隔符拆分文件?

gai*_*rlo 15 bash split file

我有这个file.csv:

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
Run Code Online (Sandbox Code Playgroud)

现在我想将这个文件分成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
Run Code Online (Sandbox Code Playgroud)

Mar*_*off 21

这个论坛上大肆窃取:

awk '/YOUR_TEXT_HERE/{n++}{print >"out" n ".txt" }' final.txt
Run Code Online (Sandbox Code Playgroud)

应该做的伎俩(YOUR_TEXT_HERE当然替换).

用您的条件替换它,并#file.txt使用以下输入文件发送输出a.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


Spi*_*ynn 7

您可以使用 csplit:

csplit file.txt /^c.*/ {*}
Run Code Online (Sandbox Code Playgroud)

此语法适用于 cygwin,但尚未在其他地方尝试过。