Help in a Recursive date program!!!!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help in a Recursive date program!!!!
# 8  
Old 03-23-2007
Quote:
Originally Posted by anbu23
Run this script once to add that days date
Code:
dt=$( date "+%b-%d-%Y" )
for i in *
do
 mv $i ${i}".${dt}"
done

Once the file has date in it use this script
Code:
dt=$( date "+%b-%d-%Y" )
for i in *
do
 mv $i ${i%.*}".${dt}"
done

Hi Anbu,

Your script is working perfectly fine.But i have got into a new problem...
Please look at these file names

BP_aaa_bbb_20070323050007.dat
BP_ccc_ddd_032307.dat
BP_aa_bb_032307.dat
BP_dd_ee_032307.dat
BP_ff_gg_032307.xls
BP_xx.yy_20070323134501.dat (apart from date,the other digits vary from day to day )
BP_ss_pp_20070323030002.dat

Now i dont know how to dynamically update the date portion of these files

Please help me in this regard.

Thanks a ton,
Kumar
# 9  
Old 03-23-2007
Quote:
Originally Posted by kumarsaravana_s
#!usr/bin/sh
#!/bin/tcsh

Do not use tcsh for scripting.
Quote:
dt=$(date"+%b-%d-%y")
[...]
I get his error mesg when i execute the script..i dont know if i'm running it wrongly..

date1.sh: syntax error at line 4: `dt=$' unexpected

You are probably using a Bourne shell. You should use a POSIX shell for scripting; there is probably one installed on your system. Check your documentation for the location.

In the unlikely event that there isn't one (it could be bash or ksh or ash), change $( ... ) to ` ... `:

Code:
dt=`date"+%b-%d-%y"`


# 10  
Old 03-24-2007
I got this script from Anbu which when run would append the date portion to the end of my files.

*********************************
dt=$( date "+%b-%d-%Y" )
for i in *
do
mv $i ${i}".${dt}"
done
***************************************

Can you please tell me how to append date of this format 032307 and 20070323 in the middle of the file name as show below and dynamically update it everyday.

TP_ccc_ddd_032307.txt
aa_bb_032307.dat
BP_dd_ee_032307.dat
BP_ff_gg_032307.xls
BP_xx_yy_20070323134501.dat

Can you please also help me with a comparison command.I mean i want to
extract only BP_xx_yy20070323.dat from the below file and compare it with a file list i have manually created.

BP_xx_yy_20070323134501.dat

Please help me in this regard.

Thanks a ton,
Kumar
# 11  
Old 03-24-2007
Quote:
Originally Posted by kumarsaravana_s
Can you please tell me how to append date of this format 032307 and 20070323 in the middle of the file name as show below and dynamically update it everyday.

You mean insert the date into the filename (append means to place at the end).
Quote:
TP_ccc_ddd_032307.txt
aa_bb_032307.dat
BP_dd_ee_032307.dat
BP_ff_gg_032307.xls
BP_xx_yy_20070323134501.dat

First you need to break the file name into its various parts: name, date and suffix:

Code:
file=TP_ccc_ddd_032307.txt
suffix=${file##*.}
name=${file%_*}
temp=${file%."$suffix"}
date=${temp#"$name"_}

Then you need to convert the date. You show two different formats, so two different conversion functions must be used. The first format is the ridiculous MMDDYY; the second, I assume, is YYYYMMDDHHMMSS.

To determine which format the date is in, check the length:

Code:
case ${#date} in
  6) six2date ;;
  14) fourteen2date ;;
esac

Then reassemble the filename and do the move:

Code:
newfile=${name}_$_DATE.$suffix
mv "$file" "$newfile"

Here are the other functions you will need:

Code:
monthname()
{
  case $1 in
    01) _MONTH=Jan ;;   02) _MONTH=Feb ;;
    03) _MONTH=Mar ;;   04) _MONTH=Apr ;;
    05) _MONTH=May ;;   06) _MONTH=Jun ;;
    07) _MONTH=Jul ;;   08) _MONTH=Aug ;;
    09) _MONTH=Sep ;;   10) _MONTH=Oct ;;
    11) _MONTH=Nov ;;   12) _MONTH=Dec ;;
  esac
}

six2date()
{
   temp=${1#??}
   year=$(( ${1%"$temp"} + 2000 )) ## Fix if last century is possible
   monthname "${temp%??}"
   day=${temp#??}
   _DATE=$_MONTH-$day-$year
}

fourteen2date()
{
   temp1=${1#????}
   year=${1%"$temp1"}
   temp2=${temp1#??}
   monthname ${temp1%"$temp2"}
   temp3=${temp2#??}
   day=${temp2%"$temp3"}
   _DATE=$_MONTH-$day-$year
}

# 12  
Old 03-27-2007
Quote:
Originally Posted by cfajohnson

You mean insert the date into the filename (append means to place at the end).

First you need to break the file name into its various parts: name, date and suffix:

Code:
file=TP_ccc_ddd_032307.txt
suffix=${file##*.}
name=${file%_*}
temp=${file%."$suffix"}
date=${temp#"$name"_}

Then you need to convert the date. You show two different formats, so two different conversion functions must be used. The first format is the ridiculous MMDDYY; the second, I assume, is YYYYMMDDHHMMSS.

To determine which format the date is in, check the length:

Code:
case ${#date} in
  6) six2date ;;
  14) fourteen2date ;;
esac

Then reassemble the filename and do the move:

Code:
newfile=${name}_$_DATE.$suffix
mv "$file" "$newfile"

Here are the other functions you will need:

Code:
monthname()
{
  case $1 in
    01) _MONTH=Jan ;;   02) _MONTH=Feb ;;
    03) _MONTH=Mar ;;   04) _MONTH=Apr ;;
    05) _MONTH=May ;;   06) _MONTH=Jun ;;
    07) _MONTH=Jul ;;   08) _MONTH=Aug ;;
    09) _MONTH=Sep ;;   10) _MONTH=Oct ;;
    11) _MONTH=Nov ;;   12) _MONTH=Dec ;;
  esac
}

six2date()
{
   temp=${1#??}
   year=$(( ${1%"$temp"} + 2000 )) ## Fix if last century is possible
   monthname "${temp%??}"
   day=${temp#??}
   _DATE=$_MONTH-$day-$year
}

fourteen2date()
{
   temp1=${1#????}
   year=${1%"$temp1"}
   temp2=${temp1#??}
   monthname ${temp1%"$temp2"}
   temp3=${temp2#??}
   day=${temp2%"$temp3"}
   _DATE=$_MONTH-$day-$year
}



Hi,

When pass the file input to this script,it is stripping off all the numerals..i dont know if i'm doing something wrong here...

Input = BP_x_yy_20070323134501.dat
Ouput = BP_x_yy_.dat

Output i'm expecting is it should replace the date with todays date...something like this..

BP_x_yy_20070327134501.dat

#!/bin/ksh

cd /tmp/sa/script

file=BP_x_yy_20070323134501.dat
suffix=${file##*.}
name=${file%_*}
temp=${file%."$suffix"}
date=${temp#"$name"_}

case ${#date} in
6) six2date() ;;
14) fourteen2date() ;;
esac

newfile=${name}_$_DATE.$suffix
mv "$file" "$newfile"

monthname()
{
case $1 in
01) _MONTH=Jan ;; 02) _MONTH=Feb ;;
03) _MONTH=Mar ;; 04) _MONTH=Apr ;;
05) _MONTH=May ;; 06) _MONTH=Jun ;;
07) _MONTH=Jul ;; 08) _MONTH=Aug ;;
09) _MONTH=Sep ;; 10) _MONTH=Oct ;;
11) _MONTH=Nov ;; 12) _MONTH=Dec ;;
esac
}

six2date()
{
temp=${1#??}
year=$(( ${1%"$temp"} + 2000 ))
monthname "${temp%??}"
day=${temp#??}
_DATE=$_MONTH-$day-$year
}

fourteen2date()
{
temp1=${1#????}
year=${1%"$temp1"}
temp2=${temp#??}
monthname ${temp1%"$temp2"}
temp3=${temp2##??}
day=${temp2%"$temp3"}
_DATE=$_MONTH-$day-$year
}
# 13  
Old 03-27-2007
Call functions with arguments
Code:
case ${#date} in
6) six2date ${date} ;;
14) fourteen2date ${date} ;;
esac

Add 1
Code:
fourteen2date()
{
temp1=${1#????}
year=${1%"$temp1"}
temp2=${temp1#??}
monthname ${temp1%"$temp2"}
temp3=${temp2##??}
day=${temp2%"$temp3"}
_DATE=$_MONTH-$day-$year
}

# 14  
Old 03-27-2007
Quote:
Originally Posted by anbu23
Call functions with arguments
Code:
case ${#date} in
6) six2date ${date} ;;
14) fourteen2date ${date} ;;
esac

Add 1
Code:
fourteen2date()
{
temp1=${1#????}
year=${1%"$temp1"}
temp2=${temp1#??}
monthname ${temp1%"$temp2"}
temp3=${temp2##??}
day=${temp2%"$temp3"}
_DATE=$_MONTH-$day-$year
}

Hi,

I did make the changes you mentioned above and when i execute it gives me an error.But the script is able to make up whether the file is six2date or fourteen2date function.

> ./test.sh
./test.sh[12]: six2date: not found
>

If i do not pass any arguments to the function,it executes without any error but the date doesnt get replaced..it is completely stripped off from the file name...

#!/bin/ksh

cd /tmp/sa/script

file=TP_xx_yy_032307.txt
suffix=${file##*.}
name=${file%_*}
temp=${file%."$suffix"}
date=${temp#"$name"_}

case ${#date} in
6) six2date ${date} ;;
14) fourteen2date ${date};;
esac

newfile=${name}_$_DATE.$suffix
mv "$file" "$newfile"

monthname()
{
case $1 in
01) _MONTH=Jan ;; 02) _MONTH=Feb ;;
03) _MONTH=Mar ;; 04) _MONTH=Apr ;;
05) _MONTH=May ;; 06) _MONTH=Jun ;;
07) _MONTH=Jul ;; 08) _MONTH=Aug ;;
09) _MONTH=Sep ;; 10) _MONTH=Oct ;;
11) _MONTH=Nov ;; 12) _MONTH=Dec ;;
esac
}

six2date()
{
temp=${1#??}
year=$(( ${1%"$temp"} + 2000 ))
monthname "${temp%??}"
day=${temp#??}
_DATE=$_MONTH-$day-$year
}

fourteen2date()
{
temp1=${1#????}
year=${1%"$temp1"}
temp2=${temp1#??}
monthname ${temp1%"$temp2"}
temp3=${temp2##??}
day=${temp2%"$temp3"}
_DATE=$_MONTH-$day-$year
}
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk program date function no longer running

I work at a company that uses a program written in AWK to track various data and prepare reports. Worked with this program for three years plus (the author is no longer with us) and the YTD Production report will not return a report with a date after 123119. This is a problem. Below is the (I... (3 Replies)
Discussion started by: paulgdavitt
3 Replies

2. Shell Programming and Scripting

Converting a date to friday date and finding Min/Max date

Dear all, I have 2 questions. I have a file with many rows which has date of the format YYYYMMDD. 1. I need to change the date to that weeks friday date(Ex: 20120716(monday) to 20120720). Satuday/Sunday has to be changed to next week friday date too. 2. After converting the date to... (10 Replies)
Discussion started by: 2001.arun
10 Replies

3. Solaris

date command, loop/recursive

is it possible to use output of one date command as input of another? I would like to know the date of Monday two weeks ago. so, the idea is that one date command subtracts two weeks, and the other finds the Monday. (2 Replies)
Discussion started by: orange47
2 Replies

4. UNIX for Dummies Questions & Answers

Short Program for Checking Server date

Hi Guys, Good morning! I have a file which looks something like this: Command was launched from partition 0. ------------------------------------------------ Executing command in server server3 Thu Jan 12 11:10:39 EET 2012 ------------------------------------------------... (3 Replies)
Discussion started by: rymnd_12345
3 Replies

5. UNIX for Dummies Questions & Answers

Script to open program and send/execute command in program

Hi, i want to write a script that executes a program (exec?) . this program then requires a filename as input. how do i give it this input in the script so the program will be complete run and close by the script. e.g. exec prog.exe program then asks for filename "enter filename:"... (1 Reply)
Discussion started by: tuathan
1 Replies

6. Programming

date calculation program

Hi One of my vendor based tool is giving date in no. of days since 1900-01-01. So, I want to display in CCYYMMDD format. For ex:- Vendor based tool is giving as 38790 days since 1900-01-01 corresponding to12/sep/2006 Does anybody has the... (1 Reply)
Discussion started by: axes
1 Replies

7. UNIX for Advanced & Expert Users

find file with date and recursive search for a text

Hey Guyz I have a requirement something like this.. a part of file name, date of modification of that file and a text is entered as input. like Date : 080206 (MMDDYY format.) filename : hotel_rates text : Jim now the file hotel_rates.ZZZ.123 (creation date is Aug 02 2006) should be... (10 Replies)
Discussion started by: rosh0623
10 Replies

8. Programming

storing date into a file from a program

hi all: i want to store the current date in to a file from a program. every time i execute the prg the date should get appended into the file. help me plz (2 Replies)
Discussion started by: bankpro
2 Replies

9. Programming

may be simple but i don't know -- Print current date from C program

How to print current date of the Unix system accessing thru C++ program ? I wrote like this #include <time.h> ....... time_t tt; struct tm *tod; .... time(&tt); tod = localtime(&tt); cout << tod->tm_mon + 1 << "/" << tod->tm_mday << "/" ... (6 Replies)
Discussion started by: ls1429
6 Replies

10. UNIX for Advanced & Expert Users

date program in ksh

#Author : kkodava #!/usr/bin/ksh #Use of this program is You can findout the no of days & day of starting and ending dates #usage no_of_days startdate<yyyymmdd> enddate<yyyymmdd> syy=`echo $1|cut -c1-4` smm=`echo $1|cut -c5-6` sdd=`echo $1|cut -c7-8` eyy=`echo $2|cut -c1-4` emm=`echo... (1 Reply)
Discussion started by: krishna
1 Replies
Login or Register to Ask a Question