Wednesday, July 4, 2007

CSV convert

#!/bin/bash
clear
if [ $# -ne 1 ]; then
echo "contoh syntax: tes.sh ";
exit 1;
fi
echo 'starting CSV convertion ..'
echo "###########################"
#echo 'ANumber BNumber StartDate StartTime Duration Zone Pulsa Partner_Op POCOrig POCTerm Trunk POTI Cek'
#echo 'ex : 1 2 3 4 5 6 7 8 9 10 11 12 13'
echo ""
echo "This script reads a CSV file n move its FIELD/COLOUM to YOUR desired ORDER. You could choose wether you wanna GET ALL field/coloum, or ONLY selected field/coloum that you INSERTED later on.If you choose ALL and the SUM of coloum doesnt match the amount of FIELD in the file, the OLD POSITION of the FIELD from the source file will be used. So if you wanted ALL COLOUM please MAKE SURE that YOU dont have a DULICATE COLOUM/FIELD generated !!!"
echo ""
echo "Also please make sure that the source file is an ASCII/TEXT delimited"
echo ""
echo "The OUTPUT of the convertion will be in `pwd`/new_$1"
echo ""
echo 'press enter key to continue..!!'
read
clear
echo 'please set the DELIMITER and MAP the fields into the required NEW ORDER ...'
echo "---------------------------------------------------------------------------"
echo ""
echo 'here are the TOP of your CSV'
echo ""
echo "-------------------------------------------------------------------------------------------------"
head $1 -n 5 #| sed -e 's/;/ ; /g'
echo "-------------------------------------------------------------------------------------------------"
#echo 'required order are : ( use space )'
#echo 'ANumber BNumber StartDate StartTime Duration Zone Pulsa Partner_Op POCOrig POCTerm Trunk POTI Cek'
echo ""
echo "step 1). please set the delimiter (ex. , ; etc) "
echo "="
read limiter
clear
echo 'please set the DELIMITER and MAP the fields into the required NEW ORDER ...'
echo "---------------------------------------------------------------------------"
echo ""
echo 'here are the TOP of your CSV'
echo ""
echo "-------------------------------------------------------------------------------------------------"
head $1 -n 5 #| sed -e 's/;/ ; /g'
echo "-------------------------------------------------------------------------------------------------"
if [ "$limiter" = "" ];then echo "muhammad.syafar@mobile-8.com"; exit 0 ;fi
echo ""
echo "step 2). please set the delimiter you WANTED(ex. , ; etc) "
echo "="
read newlimiter
if [ "$newlimiter" = "" ];then echo "muhammad.syafar@mobile-8.com"; exit 0 ;fi
clear
echo 'please set the DELIMITER and MAP the fields into the required NEW ORDER ...'
echo "---------------------------------------------------------------------------"
echo ""
echo 'here are the head of your CSV'
echo ""
echo "-------------------------------------------------------------------------------------------------"
head $1 -n 5 #| sed -e 's/;/ ; /g'
echo "-------------------------------------------------------------------------------------------------"
echo ""
echo "step 3). please set the order ( ex : 2 5 4 .. where 2,5,4 are the OLD POSITION NUMBER sequence)";echo "MUST use SPACE between FIELD ORDER n please MAKE SURE that you dont have DUPLICATE FIELD if you WANTED ALL COLOUM !!!"
echo ""
echo ""
echo ""
echo ""
echo "CHEAT CODE ORDER"
echo "AMA_MSN n AMA_Lawan FIELDS ORDER :"
echo 'ANumber BNumber StartDate StartTime Duration Zone Pulsa Partner_Op POCOrig POCTerm Trunk POTI Cek'
echo "="
read pil
if [ "$pil" = "" ];then echo "muhammad.syafar@mobile-8.com"; exit 0 ;fi
clear
field1=1
field2=2
field3=3
field4=4
field5=5
field6=6
field7=7
field8=8
field9=9
field10=10
field12=12
field11=11
field13=13
if [ -f field.list ];then
rm field.list
fi
if [ "`echo $pil | awk '{print $1}'`" != "" ];then field1=`echo $pil | awk '{print $1}'`; echo "field1 $field1" >> field.list; fi 2>/dev/null
if [ "`echo $pil | awk '{print $2}'`" != "" ];then field2=`echo $pil | awk '{print $2}'`; echo "field2 $field2" >> field.list; fi 2>/dev/null
if [ "`echo $pil | awk '{print $3}'`" != "" ];then field3=`echo $pil | awk '{print $3}'`; echo "field3 $field3" >> field.list; fi 2>/dev/null
if [ "`echo $pil | awk '{print $4}'`" != "" ];then field4=`echo $pil | awk '{print $4}'`; echo "field4 $field4" >> field.list; fi 2>/dev/null
if [ "`echo $pil | awk '{print $5}'`" != "" ];then field5=`echo $pil | awk '{print $5}'`; echo "field5 $field5" >> field.list; fi 2>/dev/null
if [ "`echo $pil | awk '{print $6}'`" != "" ];then field6=`echo $pil | awk '{print $6}'`; echo "field6 $field6" >> field.list; fi 2>/dev/null
if [ "`echo $pil | awk '{print $7}'`" != "" ];then field7=`echo $pil | awk '{print $7}'`; echo "field7 $field7" >> field.list; fi 2>/dev/null
if [ "`echo $pil | awk '{print $8}'`" != "" ];then field8=`echo $pil | awk '{print $8}'`; echo "field8 $field8" >> field.list; fi 2>/dev/null
if [ "`echo $pil | awk '{print $9}'`" != "" ];then field9=`echo $pil | awk '{print $9}'`; echo "field9 $field9" >> field.list; fi 2>/dev/null
if [ "`echo $pil | awk '{print $10}'`" != "" ];then field10=`echo $pil | awk '{print $10}'`; echo "field10 $field10" >> field.list;fi 2>/dev/null
if [ "`echo $pil | awk '{print $11}'`" != "" ];then field11=`echo $pil | awk '{print $11}'`; echo "field11 $field11" >> field.list;fi 2>/dev/null
if [ "`echo $pil | awk '{print $12}'`" != "" ];then field12=`echo $pil | awk '{print $12}'`; echo "field12 $field12" >> field.list;fi 2>/dev/null
if [ "`echo $pil | awk '{print $13}'`" != "" ];then field13=`echo $pil | awk '{print $13}'`; echo "field13 $field13" >> field.list;fi 2>/dev/null
echo 'please CHOOSE the ALL or ONLY selected FIELD/COLOUM option ...'
echo "---------------------------------------------------------------------------"
echo ""
echo 'here are the head of your CSV'
echo ""
echo "-------------------------------------------------------------------------------------------------"
head $1 -n 5 #| sed -e 's/;/ ; /g'
echo "-------------------------------------------------------------------------------------------------"
echo ""
echo "Note that on ALL (0) the unselected POSITION of FIELD will follow the source file old position"
echo "0. ALL COLOUM "
echo "1. ONLY COLOUM"
read col
if [ "$col" = "1" ];then
concat="( )"
for field in `cat field.list|cut -f2`
do
newconcat=`echo $concat | sed -e "s/ /$"$field", /g"`
concat=$newconcat
done
newconcat=`echo $concat | sed -e "s/, )/)/g"`
concat=$newconcat
#echo $concat
elif [ "$col" = "0" ];then
concat="($"$field1",$"$field2",$"$field3",$"$field4",$"$field5",$"$field6",$"$field7",$"$field8",$"$field9",$"$field10",$"$field11",$"$field12")"
else
echo "muhammad.syafar@mobile-8.com"; exit 0 ;
fi
clear
rm field.list
if [ -f new_$1 ];then
rm new_$1
fi
echo "STARTING converting YOUR CSV ... Please wait ... !!!"
echo ""
for record in `cat -sT $1| sed -e 's/-/_/g'| sed -e 's/ //g'`
do
echo "$record" | awk -F$limiter "{print $concat}"
echo "$record" | awk -F$limiter "{print $concat}"|sed -e "s/ /$newlimiter/g" >> new_$1
done
echo ""
echo "...DONE"
echo "mobile-8 IN/CCBS Interconnect div"
exit 0

0 Comment :