File splitting

Hi,

I have the below file which I need to split:

> cat file.dat
00000201703300129STK00293
0481820170329201703291131001280050200001M1051234012222981810000100000400000000500001P981804818201702020654600128LN
0481820170329201703291223001280051200003M0759999000101981810000100000000000000000088R981804818201703290000000000LN
0481820170329201703291450001280052200001M7943027010201981800000100008350000008350047R981804818201703290000000000LN
0481820170329201703291618000700081600002M2237164010702981800000100000055000000055007R981804818201703290000000000LN
0481820170329201703291621000700081700002M2571000012222981800000100000500000000360031P981804818201703290000000000LN
0481920170329201703290918002500188300001M1001212012222981900000100000500000000500017R981904819201703290000000000LN
0402120170329201703291731005050502500002M6552424000018999910000100000400000000400034R999904021201703290000000000LN
9999900000000000000000000000007

I need to create 3 files out of this.
Always the first line should go to Hfile.dat
Always the last line should go to Tfile.dat
All the remaining lines should go to Dfile.dat

Please help me in achieving this in ksh.

---------- Post updated at 04:52 PM ---------- Previous update was at 04:46 PM ----------

I can put the first line and last line using

head -1

and

tail -1

commands, but i am not sure how to get the remaining lines.

Try

tail -n+2 file.dat | tac | tail -n+2 | tac
1 Like

With sed:

sed -e '1w Hfile.dat' -e '$w Tfile.dat' -e '1d; $d' file.dat > Dfile.dat