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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1825745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:26:42  来源:igfitidea点击:

Split file based on string delimiter in bash.how?

bashfilesplit

提问by gairlo

I have this file.csv :

我有这个 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

Now I would like to split this file in 3 files , everyone whit only bloc of data

现在我想把这个文件分成 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

回答by Mark Rushakoff

Blatantly stolen from this forum:

这个论坛公然窃取:

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

should do the trick (replacing YOUR_TEXT_HERE, of course).

应该可以解决问题(YOUR_TEXT_HERE当然是替换)。

Replacing it with your conditions, and sending output to #file.txtwith an input file of a.txt:

用您的条件替换它,并将输出发送到#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

回答by Spikolynn

You could use csplit:

你可以使用 csplit:

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

This syntax works on cygwin but haven't tried it elswhere.

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

回答by Armali

This differently quoted version of the other answer also works with Windows CMD:

另一个答案的不同引用版本也适用于 Windows CMD:

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