One problem with your script, is that it should be bin, not Bin. Also you have to use double quotes around $registro to maintain appropriate spacing.
#!/bin/ksh
dir_work=/aplic/tmp
arquivo=file.txt
cd ${dir_work}
cat $arquivo | while read registro
do
campo=`echo "$registro" | cut -c38-39`
echo $registro >>${campo}_${arquivo}
done
If you want to retain the original spacing of the input file in the output files you'd also have to use double quotes:
echo "$registro" >>${campo}_${arquivo}
You can easily check what your script is doing by temporarily using
#!/bin/ksh -x
An alternative way to do it would be:
#!/bin/ksh
dir_work=/aplic/tmp
arquivo=file.txt
cd ${dir_work}
while read registro; do
campo=${registro:37:2}
echo $registro >>${campo}_${arquivo}
done<$arquivo
Thanks.... It's solved part of my problem...
I have a header and trailer.....
Header
00 02112009
Trailer
99 00000061
The shell included header in Type 02 (didn't consider the spaces on the left side) and I lost the Trailer.....
The Trailer contains total amount of register type.
$ cat file.txt |awk '{print $3}' |sort |uniq -c |sort -k2 |awk '{print $2"_file.txt with",$1, "records"}'
02_file.txt with 3 records
06_file.txt with 2 records
08_file.txt with 1 records
09_file.txt with 1 records
$ awk '{a[$3]+=1} END {for (i in a) print i"_file.txt with",a,"records" }' file.txt |sort
02_file.txt with 3 records
06_file.txt with 2 records
08_file.txt with 1 records
09_file.txt with 1 records
I use IFS= read -r instead of read and I get the results.. Thanks.
But I have other problem... the performance...
Processed 94.000 records in 3 minutes but I have 93.000.000 records.
How I solve this?
Great Solution.... and very fast
The archive has many names of input (city names)
I want to generate the output file with the same name from input.
for example:
1� input archive = saopaulo.txt
outputs: 02_saopaulo.txt / 03_saopaulo.txt / etc...