HI Guys ,
Using UNIX ,I intend to check with correct file pattern Access_file_Record.YYYYMM in path /tmp
If the file exist in correct format come out from code .
If not found check with different file patterns for same month period YYYYMM ( Like Access_file_Record_YYYYMM.txt or Access_file_Record_.YYYYMM)
Any different pattern file should be renamed to expected file Access_file_Record.YYYYMM
Every month YYYYMM pattern shoud be identified as 201201 for jan , 201202 for feb etc when script is ran for that month
If the script encounters for month 201202 (lets say) 2 files like :
Access_file_Record_201202.txt and Access_file_Record_.201202
What should the script do ?
According to what you say it should rename them with Access_file_Record_201202 but in this case after renaming Access_file_Record_201202.txt in Access_file_Record_201202 when the script encounters Access_file_Record_.201202 it will overwrite the file Access_file_Record_201202 (which has the content of Access_file_Record_201202.txt) with the content of Access_file_Record_.201202 .
this thing will happen in ANY OS. you need to clarify this first, what exactly you want. You cannot have more than 1 file with the same name on the same path.
HI As i said , the correct file pattern should be Access_file_Record.201202 .
If pattern matched exit;
If Not,then check different possible formats Access_file_Record_201202.txt or Access_file_Record_201202 etc (Access_file_Record prefix is common) are present if any . Then rename that pattern to Access_file_Record.201202 and come out . Hope i am clear with requirement.
#!/bin/bash
target_date=`date +%Y%m`
cd /tmp
the_existing_file=`ls *$target_date*`
the_desired_file="Access_file_Record.${target_date}"
if [ $the_existing_file = $the_desired_file ]; then
echo "The file was correctly named"
else
echo The file had the name "$the_existing_file" and it was renamed "$the_desired_file"
mv $the_existing_file $the_desired_file
fi
Create with vi a script and copy paste the content from above, afterwards add execution rights to the scripts. The script works for Linux environments (because of the command "date +%Y%m") which can have different outputs in different UNIXes . If you have problems with the script tell exactly what kind of Linux you use (use the uname command), usually this should be provided in the first request.
For existing entries (and apologies not the slickest production solution):
ls -1 /tmp/Access_file_Record*.* | while read line
do
IFS=.
set $line
IFS=" "
if [ $2 = "txt" ]
then
echo "mv $line $1"
mv $line $1
else
echo "mv $line $1$2"
mv $line $1$2
fi
done