No ratings yet.

How to split a file into sections in linux

1.  csplit name

csplit – split a file into sections determined by context lines

2. csplit synopsis

csplit [OPTION]... FILE PATTERN... 
3. csplit frequently used options
-f, --prefix=PREFIX
      use PREFIX instead of `xx'
-k, --keep-files
      do not remove output files on errors
-s, --quiet, --silent
      do not print counts of output file sizes 
4. csplit examples

Lets create sample file:

echo "This project's objective is not to compete with
The Linux Documentation Project , but to serve as a free
exchange reference guide for both new and established
Linux users. Visitors are encouraged to not only use the
information that is presented on this website, but to 
also share any additional information that they feel will
assist other viewers. It's main purpose is to provide 
viewers of all levels access to informative methods on 
how to configure the Linux Operating System without having
to read large amounts of documentation." > commands.txt 

Split file up to 10 parts each with one line:

csplit -k -s commands.txt 1 {10} 

Split file up to 10 parts
Do the same as above but with a prefix option:

csplit -k -s --prefix=linuxconfig.org commands.txt 1 {10} 

Split file up to 10 parts with prefix

Source taken from http://linuxconfig.org/

Please rate this