Linux csplit command: chunk the file into "xx00", "xx01"… according to the specified format. and output
Linux csplit command Function Description
Use the csplit command to chunk the file into "xx00", "xx01"… in the specified format. and output. The number of bytes in each chunked file will also be output to the standard output. The offset of a line needs to be declared as "+" or "-" after a positive integer value.
The csplit command is used to split a large file into smaller pieces and save each of the split pieces as a single file. The name of the fragment file is similar to "xx00", "xx01". csplit command is a variant of split, split can only split according to file size or number of lines, but csplit can split files according to the characteristics of the file itself. split files according to their characteristics.
Linux csplit command Syntax
csplit [Option] [File] [Format]
The meaning of each option in the command is shown in the following table.
Option | Description |
---|---|
-k |
Do not remove the wrong output file |
-n <Digital> |
Use the specified binary digits instead of binary |
-s |
Do not display the size count of the output file |
-z |
Delete empty output files |
-b <format> |
Use sprintf format instead of %02d |
-f <prefix> |
Use the specified prefix instead of "xx" |
The format that can be specified when using the csplit command is shown in the table below.
Format | Description |
---|---|
Integer | Exclude the specified line and use it as the file chunking boundary |
/expression/[offset] |
Exclude the matched lines and use them as file chunking boundaries |
%Expression %[Offset] |
Pre-skip the number of matching lines, using them as file chunking boundaries |
{Integer} |
Repeat the previously specified pattern the specified number of times |
{*} |
Repeat the previously specified pattern as many times as possible |
Linux csplit command Example
Chunk the /root/install.log file into "xx00", "xx01"…. and output, where the first file is less than 1000 lines
[root@rhel ~]# csplit /root/install.log 1000
40200
63728
[root@rhel ~]# ls /root
anaconda-ks.cfg install.log install.log.syslog xx00 xx01 公共的 模板 视频 图片 文档 下载 音乐 桌面
Chunk the /root/install.log file into "xx00", "xx01"…. and output, where the first file contains lines 1 to 9, the second file contains lines 10 to 99, the third file contains lines 100 to 999, and the fourth file contains lines 1000 to the end
[root@rhel ~]# csplit /root/install.log 10100 1000
421
3430
38897
61180
You need to split apidemos.server.log into server1.log, server2.log, server3.log, the contents of these files are taken from different SERVER sections in the original file
csplit apidemos.server.log /SERVER/ -n2 -s {*} -f server -b "%02d.log"; rm server00.log
Output: