- [Instructor] Welcome to the 10th video of section two,
splitting files and data.
In the previous video, we've learned about
temporary file naming and random numbers.
In this video, we'll see various ways of
splitting files in different chunks.
Splitting our files into many smaller pieces
becomes essential in certain situations.
Earlier when memory was limited
with devices such as floppy disks,
it was crucial to split files into smaller file sizes
to split files across many disks.
However, nowadays we split files for other purposes,
such as readability, for generating logs,
sending files over email, and so on.
Let's see how to do it.
Let's say we have a test file called Data.file,
which has a size of 100 kilobytes.
You can split this file into smaller chunks
of 10K each by specifying the split size as follows.
It will split Data.file into many files,
each of a 10K chunk.
The chunks will be named in the manner
XAB, XAC, XAD, and so on.
This means it will have alphanumeric suffixes.
To use the numeric suffixes,
use an additional minus-D argument.
It is also possible to specify a suffix length
using the minus-A length.
Instead of the K, kilobyte suffix,
we can use M for megabyte, G for gigabyte,
or C for byte, W for word, and so on.
The split command has more options.
Let's go through them.
Specifying a file name prefix for the split files.
The previous split files have a file name prefix X.
We can also use our own file name prefix
by providing a prefix file name.
The last command argument for
this split command is prefix.
It is in the format.
Let's run the previous command
with the prefix file name for split files.
To split files based on the number of lines
in each split rather than the chunk size,
use monocell number of lines as follows.
There is another interesting utility called C-split.
It can be used to split log file based on
specific conditions and string-matching options.
Let's see how to work with it.
C-split is a variant of the split utility.
The split utility can only split files based on chunk size
or based on the number of lines.
C-split makes the split based on context-based split.
It can be used to split files based on the existence
of a certain word or text content.
Look at the following example log.
We may need to split the files into server one log,
server two log, and server three log
from the contents for each server in each file.
This can be done as follows.
The details of the command are as follows.
Slash-server-slash is the line used to match
a line by which a split is to be carried out.
Slash-rejex-slash is the format.
It copies from the current line first up
on the matching line that contains Server,
excluding the match line.
Coding-brace asterisk coding-brace
is used to specify to repeat a split
based on the match up to the end of the file.
By using integer, we can specify
the number of times it's to be continued.
Minus-S is the flag to make the command silent
rather than printing other messages.
Minus-N is used to specify the number of digits
to be used as a suffix.
Zero one, zero two, zero three, and so on.
Minus-F is used for specifying
the file name prefix to split files.
Server is the prefix in the previous example.
Minus-B is used to specify the suffix format.
%02d.log is similar to the print-F argument format in C.
Here, the file name equals prefix plus suffix.
That is, server plus %02d.log.
We remove server zero zero.log,
since the first split file is an empty file.
The match word is the first line of the file.
Awesome, we've successfully learned
how to split files and data.
In the next video, we'll learn how
to slice file names based on extension.