根据分隔符将一个文件拆分为多个文件 [英] Split one file into multiple files based on delimiter
问题描述
我有一个文件,在每个部分之后以 -|
作为分隔符...需要使用 unix 为每个部分创建单独的文件.
I have one file with -|
as delimiter after each section...need to create separate files for each section using unix.
输入文件示例
wertretr
ewretrtret
1212132323
000232
-|
ereteertetet
232434234
erewesdfsfsfs
0234342343
-|
jdhg3875jdfsgfd
sjdhfdbfjds
347674657435
-|
文件 1 中的预期结果
Expected result in File 1
wertretr
ewretrtret
1212132323
000232
-|
文件 2 中的预期结果
Expected result in File 2
ereteertetet
232434234
erewesdfsfsfs
0234342343
-|
文件 3 中的预期结果
Expected result in File 3
jdhg3875jdfsgfd
sjdhfdbfjds
347674657435
-|
推荐答案
单行,无需编程.(除了正则表达式等)
A one liner, no programming. (except the regexp etc.)
csplit --digits=2 --quiet --prefix=outfile infile "/-|/+1" "{*}"
测试:csplit (GNU coreutils) 8.30
"对于 OS X 用户,请注意操作系统附带的 csplit
版本不起作用.您将需要 coreutils 中的版本(可通过 Homebrew 安装),称为 gcsplit
."— @丹尼尔
"For OS X users, note that the version of csplit
that comes with the OS doesn't work. You'll want the version in coreutils (installable via Homebrew), which is called gcsplit
." — @Danial
"补充一下,您可以让 OS X 版本正常工作(至少在 High Sierra 上是这样).你只需要稍微调整一下参数 csplit -k -f=outfile infile "/-|/+1";{3}"
.似乎不起作用的功能是 "{*}"
,我必须明确分隔符的数量,并且需要将 -k
添加到如果找不到最终分隔符,请避免它删除所有输出文件.此外,如果您想要 --digits
,则需要使用 -n
代替."— @Pebbl
"Just to add, you can get the version for OS X to work (at least with High Sierra). You just need to tweak the args a bit csplit -k -f=outfile infile "/-|/+1" "{3}"
. Features that don't seem to work are the "{*}"
, I had to be specific on the number of separators, and needed to add -k
to avoid it deleting all outfiles if it can't find a final separator. Also if you want --digits
, you need to use -n
instead." — @Pebbl
这篇关于根据分隔符将一个文件拆分为多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!