Split one file into multiple files based on delimiter
A one liner, no programming. (except the regexp etc.)
csplit --digits=2 --quiet --prefix=outfile infile "/-|/+1" "{*}"
tested on:csplit (GNU coreutils) 8.30
Notes about usage on Apple Mac
"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
"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
awk '{f="file" NR; print $0 " -|"> f}' RS='-\\|' input-file
Explanation (edited):
RS
is the record separator, and this solution uses a gnu awk extension which allows it to be more than one character. NR
is the record number.
The print statement prints a record followed by " -|"
into a file that contains the record number in its name.