[Overview]split/csplit Examples
split - split a file into pieces
split [OPTION]... [INPUT [PREFIX]]
-b <size>: put <size> bytes per output file (K, KB, MB, GB, TB)
-l <number>: put <number> lines per output file
-d: use numeric suffixes for filenames
-a <n>: use suffixes of length <n>
---
csplit - split a file into sections by context lines
csplit [OPTION]... FILE PATTERN
-b <FORMAT>: use sprintf <FORMAT> instead of %02d
-f <PREFIX>: use <PREFIX> instead of 'xx'
-n <DIGITS>: use specified number of digits instead of 2
-s: do not print counts of output file sizes
-k: do not remove files on error (an error already exists if the last file has not the desired size)
Read standard input if FILE is -. Each PATTERN may be:
INTEGER
copy up to but not including specified line number
/REGEXP/[OFFSET]
copy up to but not including a matching line
%REGEXP%[OFFSET]
skip to, but not including a matching line
{INTEGER}
repeat the previous pattern specified number of times
{*}
repeat the previous pattern as many times as possible
for further information see the manual pages split(1), csplit(1)
- split file ../data/city.sample.csv into parts of 100 lines and write the files to directory tmp with prefix city_ and a numeric suffix
split -l100 -d ../data/city.sample.csv tmp/city_
[Output], generated files: [tmp/city_00]
- split file ../data/city.sample.csv into parts of 1000 lines and write the files to directory tmp with prefix city- and a 4 character long numeric suffix. The -k option is necessarry, because the last part is smaller than 1000 lines
csplit -k -b "%04d" -f tmp/city- ../data/city.sample.csv 1000 {*}
[Output], generated files: [tmp/city-0000]
- generates 10 output files (xx00, ..., xx09) with number 101-109, 110-119, ...
seq 101 200 | csplit -k - 10 {*}
[Output], generated files: [xx00, xx01, xx02, xx03, xx04, xx05, xx06, xx07, xx08, xx09, xx10]
- splits input file at pattern /0$/. Generates 10 output files (xx00, ..., xx09) with number 101-109, 110-119, ...
seq 101 200 | csplit -f tmp/xx - /0$/ {*}
[Output], generated files: [tmp/xx00, tmp/xx01, tmp/xx02, tmp/xx03, tmp/xx04, tmp/xx05, tmp/xx06, tmp/xx07, tmp/xx08, tmp/xx09, tmp/xx10]
- Different patterns with copy and skip semantic. seq-demo00: 101-108, then skip up to (but not include) 113, seq-demo01: 113-116, skip up to 118 (not included), seq-demo02: 118-119, seq-demo03: 120-123
seq 101 123 | csplit - /109/ %113% /117/ %118% /120/ -f tmp/seq-demo
[Output], generated files: [tmp/seq-demo00, tmp/seq-demo01, tmp/seq-demo02, tmp/seq-demo03]
- gernerate a file with prefix tmp/chapnum for every chapter of the book moby-dick.txt (the first two %...%-patterns are needed to skip the table of contents).
csplit -f tmp/chap ../data/moby-dick.txt '%^Epilog%' '%^CHAPTER 1%' /^CHAPTER/ {*}
[Output], generated files: [tmp/chap00, tmp/chap01, tmp/chap02, tmp/chap03, tmp/chap04, tmp/chap05, tmp/chap06, tmp/chap07, tmp/chap08, tmp/chap09, tmp/chap10, tmp/chap100, tmp/chap101, tmp/chap102, tmp/chap103, tmp/chap104, tmp/chap105, tmp/chap106, tmp/chap107, tmp/chap108, tmp/chap109, tmp/chap11, tmp/chap110, tmp/chap111, tmp/chap112, tmp/chap113, tmp/chap114, tmp/chap115, tmp/chap116, tmp/chap117, tmp/chap118, tmp/chap119, tmp/chap12, tmp/chap120, tmp/chap121, tmp/chap122, tmp/chap123, tmp/chap124, tmp/chap125, tmp/chap126, tmp/chap127, tmp/chap128, tmp/chap129, tmp/chap13, tmp/chap130, tmp/chap131, tmp/chap132, tmp/chap133, tmp/chap134, tmp/chap14, tmp/chap15, tmp/chap16, tmp/chap17, tmp/chap18, tmp/chap19, tmp/chap20, tmp/chap21, tmp/chap22, tmp/chap23, tmp/chap24, tmp/chap25, tmp/chap26, tmp/chap27, tmp/chap28, tmp/chap29, tmp/chap30, tmp/chap31, tmp/chap32, tmp/chap33, tmp/chap34, tmp/chap35, tmp/chap36, tmp/chap37, tmp/chap38, tmp/chap39, tmp/chap40, tmp/chap41, tmp/chap42, tmp/chap43, tmp/chap44, tmp/chap45, tmp/chap46, tmp/chap47, tmp/chap48, tmp/chap49, tmp/chap50, tmp/chap51, tmp/chap52, tmp/chap53, tmp/chap54, tmp/chap55, tmp/chap56, tmp/chap57, tmp/chap58, tmp/chap59, tmp/chap60, tmp/chap61, tmp/chap62, tmp/chap63, tmp/chap64, tmp/chap65, tmp/chap66, tmp/chap67, tmp/chap68, tmp/chap69, tmp/chap70, tmp/chap71, tmp/chap72, tmp/chap73, tmp/chap74, tmp/chap75, tmp/chap76, tmp/chap77, tmp/chap78, tmp/chap79, tmp/chap80, tmp/chap81, tmp/chap82, tmp/chap83, tmp/chap84, tmp/chap85, tmp/chap86, tmp/chap87, tmp/chap88, tmp/chap89, tmp/chap90, tmp/chap91, tmp/chap92, tmp/chap93, tmp/chap94, tmp/chap95, tmp/chap96, tmp/chap97, tmp/chap98, tmp/chap99]
all examples assembled by andreas schmidt for the IC3K 2022 conference