Need help using sed inside the loop.

Hi,

i have written a script. it collects data based on the sql queries executed by it. i have multiple output files. after the output file is made i need to do some cosmetic changes in the files and then store them. i am unable to use sed conditions inside the loop.

see below code for storing the files.

 
 
backup_files()
{
for file in `ls tmp*`
do
mv $file `echo $file |sed 's/tmp/fnl/'`
if [[! -d "${BACKUP_DIR}" ]]
then
mkdir "${BACKUP_DIR}" && chmod 744 "${BACKUP_DIR}"
fi
mv fnl* "${BACKUP_DIR}"
done
}

nw see the code for doing the cosmetic changes in the output file, - this has to be done on each and every file. before taking its backup

 
cleanup()
{
sed -n '1,$s/^ *//gp' INFORM_${DATE}_${id} | sed -n '1,$s/ *|/|/gp' | sed -n '1,$s/| */|/gp' | sed -n '1,$s/ *$//gp' >  INFORM_${DATE}_${id}.tmp
mv INFORM_${DATE}_{id}.tmp INFORM_${DATE}_{id}
sed -n '1,$s/NULL//gp' INFORM_${DATE}_{id} > INFORM_${DATE}_{_id}.tmp
mv INFORM_${DATE}_{id}.tmp INFORM_${DATE}_{id}
}

can some1 please help me with this. i am not sure how will i implement all the sed operations on all the files.
moreover, i am putting date here at the end of the file as suffix i want date in the below format but i unable to get tht .. please help me out with tht also.

 
date "+%Y%m%d%H%M%S"

not able to get this format at the end of the file.

In the first case "backup_files" are you getting any error?

The same works fine with me.

Please also post , the input and the output you are expecting.

Regards
Ravi Sastry

You have got lots of typos ( not sure they also appear in the actual code).

cleanup()
{
sed -n '1,$s/^ *//gp' INFORM_${DATE}_${id} | sed -n '1,$s/ *|/|/gp' | sed -n '1,$s/| */|/gp' | sed -n '1,$s/ *$//gp' >  INFORM_${DATE}_${id}.tmp
mv INFORM_${DATE}_${id}.tmp INFORM_${DATE}_${id}
sed -n '1,$s/NULL//gp' INFORM_${DATE}_${id} > INFORM_${DATE}_${id}.tmp
mv INFORM_${DATE}_${id}.tmp INFORM_${DATE}_${id}
}

Not sure what you are trying to replace..
but If you do want to apply this to each file, make this function to accept filename as the argument.

then call it inside the loop in this way,

cleanup $file

Also, there is "useless use of ls",

for file in `ls tmp*`

should be changed to:

for file in tmp*

there must be a space before the "!" in the test command.

regarding date, use something like this

DATE=$(date "+%Y%m%d%H%M%S")

anchal sorry for typos. they are not in actual code, i always change the code variables before i pase them here. thats why all these typos.
i m going to run what all u said and will tell u the output soon.

thanks a lot