I'm trying to read a file, and parse it in bash. I need to do a dd
to convert from EBCDIC
to ASCII
, then loop and read X bytes, piping each X bytes as a row in a new file:
#!/bin/bash
# $1 = input file in EBCDIC
# $2 = row length
# $3 = output file
# convert to ASCII and replace NUL (^@) with ' '
dd conv=ascii if=$1 | sed 's/\x0/ /g' > $3.tmp
file=$(cat "$3.tmp")
sIndex=1
fIndex=$2
# remove file
rm $3
echo "filesize: ${#file}";
# loop, retrieving each fixed-size record and appending to a file
while true; do
# append record to file
echo "${file:sIndex:fIndex}" >> $3;
# break at end of file
if [ $fIndex -ge ${#file} ]
then
break;
fi
# increment index
sIndex=$((sIndex+fIndex));
done
# remove tmp
rm $3.tmp
Any way to make this whole process faster?