Readers like you help support MUO. If you make a purchase through links on our site, we may receive an affiliate commission. Continue reading.
csplit is a popular Linux command line utility used to split the contents of a file into two parts. The file you need to change must be a text file with a “.txt” Renewal.
The command is easy to use and works well on all Linux distributions. By using different flags available for csplit you can also change the output according to your needs.
How to use csplit to split a file on Linux.
Table of Contents
What is csplit?
csplit is used on Linux and other Unix-like operating systems and can split a file into individual files determined by context lines.
The basic syntax of the command is:
csplit [OPTION] [PATTERN]
csplit vs split
Most Linux users like to use the split command when it comes to splitting a file into several smaller files. The problem with this command is that it relies on byte size or line size to split the files.
This is not possible in scenarios where you want to split the files based on their content and not their size. This is where csplit comes to the rescue as it splits the file into fixed-size chunks based on content instead of byte count.
How to install csplit on linux
csplit is pre-installed on almost all Linux distributions. However, when you face csplit: command not found error, it means that the tool is not installed on your system. To install csplit on Ubuntu, run:
sudo apt-get install coreutils
On Arch Linux, run:
sudo pacman -S coreutils
To install csplit on Fedora and RHEL:
sudo dnf install coreutils
How to use csplit on Linux
To see how csplit works, create a text file on your system. Use the touch command to create an empty file.
touch filename.txt
After creating the file, open it with the nano editor to modify its content.
nano filename.txt
After adding some content to the file, press Ctrl + X and then Y save and close.
To inspect the file contents using the cat command, run:
cat filename
Use the csplit command to split a file
To understand how csplit works, first look at the contents of the file used here as an example.
The file contains nine lines numbered 1 through 9. If you need to split the file in two, how do you tell csplit what to send to the first file and what to send to the other? That’s easy. In the command, you just have to tell csplit from which line to start splitting.
This is done by specifying the line number. For example, if you want to split the file starting with the third line with the word “London”, specify 3 in the command. Enter the command as follows:
csplit filename.txt 3
This command immediately splits the file into two parts. Use the ls command to list all directory contents to view the output files. You will find the new files with the names xx00 and xx01 next to the original file.
Use the cat command to inspect the contents of both files.
As you can see, csplit split the file into two parts starting from the third line as specified in the command.
The csplit command options
Here are some of the csplit command line options you can use:
1. Change the prefix for output files
Also known as a prefix flag, -f changes the prefix in the file name. You may have noticed when csplit splits the file that newly created files have xx as a prefix in the file names. You can change this by using the -f flag in the command.
For example, if you want the filenames ABC as a prefix xxissue the command as follows:
csplit -f abc filename.txt 3
As can be seen, after the split both have files ABC as a prefix in the names.
2. Keep the files if errors occur
That -k or the –keep files option does not remove the output files if there is an error in the csplit command.
Issue the following erroneous command:
csplit -k randomfile.txt 2 {3}
3. Change the number of characters in the file name
This option lets you tell the csplit command how many digits you want to see after the prefix in the filename. It is also called a digit flag.
To keep only one digit in the filename, issue the following command:
csplit -n 1 randomfile.txt 2
Without that -n Flag, by default you see two digits in the filename.
4. Split the file without giving out the size count
Also known as the calm flag, the -s Flag silently splits the file without mentioning the size count of the output files.
csplit -s randomfile.txt 3
5. View the command line help
To see details about all the options available for csplit, use the -H or –Help flag in the command.
csplit --help
6. Check the csplit version number
To see what version of csplit you are using, run the command with the –Execution Flag:
csplit --version
7. When sharing, omit a specific line
You can also use the –suppress-matched Command line option to omit a specific line when splitting the file.
csplit --suppress-matched filename.txt 5
When creating the two files, csplit ignores the fifth line and splits the file from the next line.
In the fifth line of the original file is the word “Berlin”. “Berlin” was omitted from the output file.
Effortlessly share files with a single command on Linux
There are many command line utilities for managing files on a Linux system. One of them is csplit. By default, it is available on all Linux systems. If not, you can easily install it from the command line.
csplit is an easy and efficient way to split a file when you need to split the file based on its content. csplit comes with various command line options that give you the flexibility to customize the output to your liking. There are several command line tools to view the contents of a file on Linux if you want to inspect the files after splitting.
This article was previously published on Source link