Shell script to manipulate files

My requirement is explained below:

list of files available in server 1 in path /home/xxx/src are:

XX_SRC_20130417.txt
XX_SRC_20130417.dat
$cat XX_SRC_20130417.txt
col1=ABC
col2=
col3=xyz

a sequence file name is maintained which is in the path /ab_app/xx/seq

$cd /ab_app/xx/seq
$cat val.txt
xm|20130104|001|0
xy|20130704|002|1 
xx|20130405|002|1
xz|20121213|001|1

Rule: Get the latest sequence value from the file for xx ie, 002.increment it by 1 (it will be 003) and store it in a variable.Make another copy of the file in /home/xxx/src renaming the file like below:

XX_SRC_20130417_003.txt
XX_SRC_20130417_003.dat

003 -> is the incremented number here.

Also, update the file XX_SRC_20130417_003.txt content with the incremented value

col1=ABC
col2=003
col3=xyz

sftp the newly created file.
If sftp is successful (used scp command to transfer files) for all the newly created two files sent( sent to server 2 , path: /extract/src/xx) then only update the content of val.txt with the incremented value.

After update, It should be something like:

xm|20130104|001|0
xy|20130704|002|1 
xx|20130405|003|1             <---------------------------value updated here 
xz|20121213|001|1

Remove the files below from /home/xxx/src(server1) no matter whether sftp is succeed or failed

XX_SRC_20130417_003.txt
XX_SRC_20130417_003.dat

Need your help and guidance to get the desired output.

Thanks in advance.

---------- Post updated at 03:19 PM ---------- Previous update was at 02:24 PM ----------

I have made a draft of my approach something like below:

export source_dir=/home/xxx/src
file_from_list=`ls  $source_dir/$file_name`
new_file_list=`ls  $source_dir/$new_file_name`

send_sftp(){

    scp $file_to_move $conn_string
    if [[ $? -ne 0 ]]
    then
        print "++++ File Transfer failed at `date +'%Y%m%d %H:%M%S'` " ;
        print "++++ File transfer failed at `date +'%Y%m%d %H:%M%S'`"
        exit 14
    fi

    rm $file_to_move ## irrespective of success or failure of sftp remove the new files
    if [[ $? -ne 0 ]]
    then
        print "++++ ERROR: File Transfer to  failed  at `date +'%Y%m%d %H:%M%S'` " ;
        print "++++    Transferred file could not be removed"
        exit 15
    fi
  


}
if [[ $application_id = "2" ]]
then
    print "Entering here..."
    conn_string=xxx@myserver2:/home/xxx/src
    file_name=xx_*
fi

value=grep "xx" val.txt| cut -d"|" -f2 
get_seq_val=$value+1
if [[ $application_id = "2" ]]
then
for list_files in $file_from_list
do
   fl_name=`basename list_files`
   fl=`echo $fl_name|cut -d'.' -f1`
   extn=`echo .${fl_name#*.}`
   new_file_name=$fl"_"$get_seq_val$extn   
   cp $list_files $new_file_name
done

for new_files in $new_file_list
do
   file_to_move=$new_files
   send_sftp()
   ### if all the files trasferred successfully then update the value in value.txt file ?? should we use sed to update the file? Can we track if all the files
   # are sftp ed successfully?
done
fi

I am new to unix world. I need your help here.