Linux csplit command - chunk the file

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:

Linux csplit command

Like(1)

Related

Linux Login Logout Command
Linux login commandLinux logout commandLinux nologin commandLinux exit commandLinux sulogin commandLinux rlogin commandLinux poweroff commandLinux ctrlaltdel CommandLinux shutdown commandLinux halt commandLinux reboot commandLinux init commandLinux runlevel commandLinux telinit command
Linux File Management Command
Linux cat commandLinux tac commandLinux nl commandLinux more commandLinux less commandLinux head commandLinux tail commandLinux rev commandLinux fold commandLinux fmt commandLinux expand commandLinux pr commandLinux sort commandLinux uniq commandLinux cut commandLinux comm commandLinux diff commandLinux join commandLinux diff3 commandLinux cmp commandLinux colrm commandLinux paste commandLinux mkdir commandLinux tr commandLinux split commandLinux csplit commandLinux tee commandLinux unexpand commandLinux patch commandLinux awk commandLinux sed commandLinux od commandLinux pwd commandLinux cd commandLinux ls commandLinux dir commandLinux dirs commandLinux touch commandLinux rmdir commandLinux cp commandLinux mv commandLinux rm commandLinux install commandLinux tmpwatch commandLinux file commandLinux du commandLinux wc commandLinux tree commandLinux cksum commandLinux md5sum commandLinux sum commandLinux dirname commandLinux mkfifo Command
Cron Expressions
Cron Expression to Run Every Day at 12 PMUnderstanding Vue Cron ExpressionsUnderstanding JS Cron ExpressionsA Comprehensive Guide to Cron Expressions for Scheduled TasksUnderstanding Linux Cron ExpressionsUnderstanding Quartz Cron ExpressionsCron ExpressionCron Time ExpressionCron Expression ParsingCron Expression: Executing a Task Every SecondCron Expression for Every Minute ExecutionCron Expression to Execute Every 10 MinutesCron Expression: Executing Every HourCron Expression to Execute Once a YearCron Expression: How to Schedule a Task to Run Daily at Midnight?