Split file into multiple files depending upon first 4 digits

Hi All,

I have a file like below:

1016D"ddd","343","1299"
1016D"ddd","3564","1299"
1016D"ddd","3297","1393"
1016D"ddd","32989","1527"
1016D"ddd","346498","1652"
2312D"ddd","3269","1652"
2312D"ddd","328","1652"
2312D"ddd","2224","2100"
3444D"ddd","252","2100"
3444D"ddd","2619","2100"

I need to split this big file (will contain lot of records like above) into multiple files depending upon the first 4 digits. The output files will be like:

filename.1016.dat

1016D"ddd","343","1299"
1016D"ddd","3564","1299"
1016D"ddd","3297","1393"
1016D"ddd","32989","1527"
1016D"ddd","346498","1652"

filename.2312.dat

2312D"ddd","3269","1652"
2312D"ddd","328","1652"
2312D"ddd","2224","2100"

filename.3444.dat

3444D"ddd","252","2100"
3444D"ddd","2619","2100"

I searched for some examples but they all are done using awk.. and as there is no field separator here I cant use awk..

Please help.

Thanks
D

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

Hi,

many thanks.. it worked.
Please could you tell me what the first line of the code does

awk '!/^$/{

Cant I use the code without the !/^$/ part ?

D

awk '!/^$/ will ignore empty lines.

ghostdog74 U:)win

Thanks Mate..

And excuse me if it sounded like a dumb quest :slight_smile: