Hi ,
I am having a scenario where I need to split the file based on two field values. The file is a fixed length file.
ex:
AA0998703000000000000190510095350019500010005101980301
K 0998703000000000000190510095351019500020005101480
CC0338703368396368396190510114449019600010005101980301
L 03387033683963683961905101144500196000200051012803
I O553203000000000000190510120433019700010005101980301
Split based on 4th position to 4 char (9987) and 21st to 6 char(190510)
So, in the above example there will be 3 files generated.
#!/bin/bash
outfile="" prefile=""
while IFS= read line
do
outfile="${line:3:4}_${line:21:6}.txt"
if [ "$outfile" != "$prefile" ]
then
exec >>"$outfile"
prefile=$outfile
fi
echo "$line"
done < filename
It tries to reduce the number of open() calls.
The same is achieved in awk by minimizing the close()