Files – Split File by Number of Lines Including Header in Each One

awkfilessplit

I need to split a .txt file into smaller ones containing 100 lines each, including the header. I don't know if this is relevant, but the original file is delimited like this:

COLUMN1 | COLUMN2 | COLUMN3
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9

I need every file generated from this split to have the header line. Also, they need to be generated in/moved to another directory and follow a name pattern, like file_01.txt, file_02.txt, etc

Best Answer

With gnu split you could save the header in a variable then split starting from the 2nd line, using the --filter option to write the header first and then the 99 lines for each piece and also specify the output directory (e.g. path to/output dir/):

header=$(head -n 1 infile.txt)
export header
tail -n +2 infile.txt | split -l 99 -d --additional-suffix=.txt \
--filter='{ printf %s\\n "$header"; cat; } >path\ to/output\ dir/$FILE' - file_

this will create 100-lines pieces as

path to/output dir/file_01.txt
path to/output dir/file_02.txt
path to/output dir/file_03.txt
..............................
Related Question