1826604 Members
3491 Online
109695 Solutions
New Discussion

Script

 
Shikha Punyani
Advisor

Script

Hello All,

I have to create a script where I have to move a file name of type FILEXXX_XXX_XXXX.TXT_YYMMDDHHMM.gz from one directory to another directory and once moved, I want to gunzip the file and remove the timestamp.
Have to run it in KSH shell.

I am new to scripting ..Can anyone please suggest ASAP?
9 REPLIES 9
Jim Walls
Trusted Contributor

Re: Script

You could use something like this:

srce=./test
dest=./test2
for file in $(ls $srce)
do
newf=$(echo $file | sed 's/\.TXT_.*\.gz//').TXT.gz
mv $srce/$file $dest/$newf
gunzip $dest/$newf
done
Shikha Punyani
Advisor

Re: Script

Hello Jim, Thanks a lot for your quick response.
I am very new to this.

I have this request to perform :
Move the file under
/u03/UBIDW/data/BART/FILE135_PI_TURNAWAYS.TXT_YYMMDDHHMM.gz to /u03/UBIDW/data/inbound -gunzipping the file and removing the _YYMMDDHHMM extension.

/u03/UBIDW/data/BART is the source
/u03/UBIDW/data/inbound is the destination.
File has a fixed value which is FILE135_PI_TURNAWAYS.TXT_YYMMDDHHMM.gz. It will always come on our server in this format.

1. When it will move to inbound, it should gunzip and remove the timestamp. That is the task which I need to perform...


Second thing is :
we already had scripts for such tasks. an example of that script is :


exec 1>>/oracle/EBAIX/files/inputs/xxtng/mis/logs/HKP_COM_AIX_EBW04.log 2>>/oracle/EBAIX/files/inputs/xxtng/mis/logs/HKP_COM_
AIX_EBW04.log

#csc_startmsg

STATUS=$1
filelist="$2"
#STATUS=SUCCESS
#filelist=FILE134_PI_RESERVATION_OR_STAY.TXT

#echo $STATUS
#echo $filelist
###############################################################################
# Begin the Main process

if [ "$STATUS" = "SUCCESS" ]
then
cd /u03/PBIDW/data/backup;
pattern="${filelist}[_,.]`date '+%y%m%d'`"
filename=`ls -1 ${pattern}*`
rc=$?
if [ $rc -ne 0 ]
then
echo "Today's file has yet not arrived";
exit 6;

else
if [ `echo $filename | wc -w` -ne 1 ]
then
echo "More than one file has arrived today. Check with Ops Analyst"
exit 6;
fi

cp /u03/PBIDW/data/backup/${filename} /u03/PBIDW/data/inbound
rc=$?
if [ $rc -ne 0 ]
then
echo "Copy of file $filename to /u03/PBIDW/data/inbound failed"
exit 6
else
echo "Copy of file $filename to /u03/PBIDW/data/inbound was successful"

fi

cd /u03/PBIDW/data/inbound;
new_filename=${filename%TXT*}"TXT"
echo $new_filename
mv $filename $new_filename
echo "new Filename = "$new_filename
fi
else
for filename in `cd /u03/UBIDW/data/inbound;ls -1 $filelist`
do
mv /u03/UBIDW/data/inbound/${filename} /u03/UBIDW/data/error/${filename}_`date '+%y%m%d%H%M%S'`
rc=$?
if [ $rc -ne 0 ]
then
echo "Move of file $filename to /u03/UBIDW/data/error failed"
exit 6
else
echo "Move of file $filename to /u03/UBIDW/data/error was successful"
fi
done
fi

exit 0


I have to make modifications in this script to run that task meantioned above...

Any suggestions what changes I have to made to this scripts?

Thanks a ton for your support :)
Jim Walls
Trusted Contributor

Re: Script

Without actually doing the modifications for you, I can explain what the steps in my snippet do... you can then use that information to modify your script as you require.

#Set up the source and destination directories
srce=/u03/UBIDW/data/BART
dest=/u03/UBIDW/data/inbound


# Assuming you have located the source file:
# perhaps using the ls command.

cd $srce
pattern="${filelist}[_,.]$(date '+%y%m%d')"
filename=$(ls -1 ${pattern}*)
.
.
.

# first generate a new name - remove everything between ".TXT_" and ".gz" - ie. the date-time stamp
newfile=$(echo $filename | sed 's/\.TXT_.*\.gz//').TXT.gz

# Now move the file to the destination
mv $srce/$filename $dest/$newfile

# And, finally, uncompress it
gunzip $dest/$newfile


Shikha Punyani
Advisor

Re: Script

Jim
That was such a quick response, I will try to make script in office today.
Thanks for making the meaning of the lines clear to me.
You have included everyting in script :)


Best Regards
Shikha
Shikha Punyani
Advisor

Re: Script

Hello Again,

I have written the script in test enviorment and it looks like this:

I have attached it in a notepad..Can you please check if I have properly closed loops of IF by doing fi and can you see any other typo error in it?

Thanks Again :)

James R. Ferguson
Acclaimed Contributor

Re: Script

HI:

Scripts shuld begin with a "she-bang" line that defines the interpreter to use. Add:

#!/usr/bin/sh

...to yours. Then, verify the syntax with:

# sh -n ./scriptname

Regards!

...JRF...
Shikha Punyani
Advisor

Re: Script

Hi James:
I apologize, I pasted it wrong , actually it loooks like the file which is attached now .
Shikha Punyani
Advisor

Re: Script

I will let you guys know whether its executing fine or not.
However I will go to office after 3 days now.
Apologies for delay.

This was my first script.
Thanks a ton for helping me with this. that too with quick response :)
Shikha Punyani
Advisor

Re: Script

Hi all it is working now...Thanks all for your help