Split large file and add header and footer to each small files

I have one large file, after every 200 line i have to split the file and the add header and footer to each small file?
It is possible to add different header and footer to each file?

Post sample data and example of the desired output.

Content of large file:-

E106,0,1/9/1993,0,E001,E003,A,45200,3766.667,21.730769
E108,0,2/3/1995,0,E001,E003,A,15000,1250,7.211538
E109,0,06-mar-07,0,E001,E001,A,78000,6500,37.5
E110,0,09-dec-2008,0,E001,E001,A,56000,4666.667,26.923077
E104,0,06/04/1994,0,E001,E003,A,95000,7916.667,45.673077
E105,0,7/30/1993,0,E001,E003,A,87000,7250,41.826923
E106,0,1/9/1993,0,E001,E003,A,45200,3766.667,21.730769
E108,0,01-feb-2008,0,E001,E003,A,15000,1250,7.211538
E109,0,2/15/1995,0,E001,E001,A,78000,6500,37.5

small file contain:-
file1.txt
E001 start
E106,0,1/9/1993,0,E001,E003,A,45200,3766.667,21.730769
E108,0,2/3/1995,0,E001,E003,A,15000,1250,7.211538
E001 End

file2.txt
E002 Start
E109,0,06-mar-07,0,E001,E001,A,78000,6500,37.5
E110,0,09-dec-2008,0,E001,E001,A,56000,4666.667,26.923077
E002 End

You can use something like this:

awk 'END { printf "E%03d end\n", c > f }
!(NR % 200) || NR == 1 { if (f) { printf "E%03d end\n", c > f; close(f) } 
printf "E%03d start\n", ++c > (f = "file" c ".txt") }
{ print > f }' large

Use nawk or /usr/xpg4/bin/awk on Solaris.

1 Like

Hi radoulov

Could you please explain you code?

what to do if header and footer remain the same in every small file.

thanks

Should they remain the same across all small files?
The code I posted generates the same start and end for every single file.

Perhaps you can adapt something like this to do what you want:

 $ split -d -l 3 temp.txt file && for X in file*; do { echo "$X start"; cat $X; echo "$X end"; } > $X.txt; done

Explanation:

Splits the file every three lines, naming each split-off file with "file" followed by digits. The for loop then takes each file, writes the "start" section, the contents of the file, then the "end" section, and names it with the same file name but with a ".txt" at the end.

Working example:

 $ cat temp.txt 
E106,0,1/9/1993,0,E001,E003,A,45200,3766.667,21.730769
E108,0,2/3/1995,0,E001,E003,A,15000,1250,7.211538
E109,0,06-mar-07,0,E001,E001,A,78000,6500,37.5
E110,0,09-dec-2008,0,E001,E001,A,56000,4666.667,26.923077
E104,0,06/04/1994,0,E001,E003,A,95000,7916.667,45.673077
E105,0,7/30/1993,0,E001,E003,A,87000,7250,41.826923
E106,0,1/9/1993,0,E001,E003,A,45200,3766.667,21.730769
E108,0,01-feb-2008,0,E001,E003,A,15000,1250,7.211538
E109,0,2/15/1995,0,E001,E001,A,78000,6500,37.5

 $ split -d -l 3 temp.txt file && for X in file*; do { echo "$X start"; cat $X; echo "$X end"; } > $X.txt; done

 $ cat file00.txt
file00 start
E106,0,1/9/1993,0,E001,E003,A,45200,3766.667,21.730769
E108,0,2/3/1995,0,E001,E003,A,15000,1250,7.211538
E109,0,06-mar-07,0,E001,E001,A,78000,6500,37.5
file00 end

 $ cat file01.txt
file01 start
E110,0,09-dec-2008,0,E001,E001,A,56000,4666.667,26.923077
E104,0,06/04/1994,0,E001,E003,A,95000,7916.667,45.673077
E105,0,7/30/1993,0,E001,E003,A,87000,7250,41.826923
file01 end

 $ cat file02.txt
file02 start
E106,0,1/9/1993,0,E001,E003,A,45200,3766.667,21.730769
E108,0,01-feb-2008,0,E001,E003,A,15000,1250,7.211538
E109,0,2/15/1995,0,E001,E001,A,78000,6500,37.5
file02 end


I have a similar task I want to perform, however, I don't need a footer and my header will be the same every time. Also, my header is multiple lines long. I imagine I would just write:

 $ split -d -l 3 temp.txt file && for X in file*; do { echo "header"; cat $X; } > $X.txt; done

I don't know how to deal with the line breaks though. Can anyone help?