I've written this in ksh but due to the large number of files (millions) it should be faster in C but unsure how to do things like year=${file:$yearStart:4} though I know I need to use strlen but do I need strcomp for the ${file:$yearStart:4} syntax? And how do you strip off a path like I'm doing with fpath=${data[c]%/*} and only the file name without path with file=${data[c]##*/} ?
Thanks!
Thanks!
Code:
#!/bin/ksh
clear
set -A data $(cat /tmp/data.txt)
numdata=${#data[@]}
MAINDIR=/export/data
DIR=newdata
CP=/bin/cp
i=0; c=0
while (( i < numdata ))
do
file=${data[c]##*/}
fpath=${data[c]%/*}
filelen=${#file}
s=0
while (( s < filelen ))
do
x=${file:$s:1}
((t=s+1))
tt=${file:$t:4}
if [[ $x = "#" && $tt = 2008 ]]
then
((yearStart=s+1))
((monthStart=s+5))
year=${file:$yearStart:4}
month=${file:$monthStart:2}
typeset -R2 yearend=$year
if [[ $year = 2008 ]]
then
case $month in
01) MONTH=Jan ;;
02) MONTH=Feb ;;
03) MONTH=Mar ;;
04) MONTH=Apr ;;
05) MONTH=May ;;
06) MONTH=Jun ;;
07) MONTH=Jul ;;
08) MONTH=Aug ;;
09) MONTH=Sep ;;
10) MONTH=Oct ;;
11) MONTH=Nov ;;
12) MONTH=Dec ;;
esac
fi
fi
((s+=1))
done
tput cup 50 0; echo -n "$i"
#echo "Copying $MAINDIR${fpath}/${file} to /${year}/${MONTH}${yearend}/${DIR}"
${CP} $MAINDIR${fpath}/${file} /${year}/${MONTH}${yearend}/${DIR} 2>/dev/null
((c+=1))
((i+=1))
done
echo
echo "Copied $numdata files"
exit 0