Split a big file into multiple files based on first four characters

I have a requirement to split a huge file to smaller text files based on first four characters which look like
ABCD
1234
DFGH
RREX
:
:
:
:
:
0000

Each of these records are OF EQUAL bytes with a different internal layout based on the above first digit identifier..

Any help to start up with a command or shell script is very much appreciated.

awk '{F=substr($0,1,4)".txt";print >> F;close(F)}' input_file
1 Like

As we are talking 17 files only, the close although correct could be ommitted:

$ awk '{print > substr ($0,1,4)}' file
1 Like

Thanks guys for quick turnaround !!

---------- Post updated at 01:39 PM ---------- Previous update was at 09:44 AM ----------

I was using this awk command on the input file ($1) which is working good but

awk '!/^$/{
 a=substr($0,1,4)
 print $0 > a".txt"
}' $1

this is giving me the output file names as a.txt ..My requirement is to have somethng like ACH.a.txt (ACH appended to all the file names)

print $0 > "PREFIX." a ".txt"
1 Like

great and thanks corona ! that worked awesome ..