Split file by number of lines including header in each one

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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
..............................

Method 2

awk 'NR==1        {a=$0}
    (NR-1)%100==0 {print a > "d/file_" int(1+(NR-1)/100)}
                  {print   > "d/file_" int(1+(NR-1)/100)}'

Method 3

Works for me in bash:

lines=100; { read header && sed "1~$((${lines}-1)) s/^/${header}n/g" | split -l $lines --numeric-suffixes=1 --additional-suffix=.txt - file_ ; } < inputfile.txt


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x