change date format and then compare dates


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting change date format and then compare dates
# 1  
Old 07-24-2009
Question change date format and then compare dates

I have filenames
filenameA_fg_MMDDYY.tar.gz
filenameASPQ_fg_MMDDYY.tar.gz
filenameAFTOPHYYINGH_fg_MMDDYY.tar.gz
filenameAGHYSW_fg_MMDDYY.tar.gz


My requiremnt needs to extract date which is in MMDDYYand change it into YYYYMMDD format.

I have done the following:
filedate=`echo $filename | awk -F"[_.]" '{print $(NF-2)}'`
formatdate = `echo $filedate | awk 'BEGIN {OFS=""}{print 20 substr($1,5,2), substr($1,1,2), substr($1,3,2)}'`

if [ formatdate -lt myowndate ]

This comparison doesnot work . I have tried putting quotes, < everything nothing works. I understand the problem is formatdate datetype is actually string and so it is not doing the compare.

Is there a way where I can change the MMDDYY date to YYYYMMDD number format . Basically what i need is the YYYYMMDD format to be number datatype . Any solution . I apologise if im not clear.SmilieSmilieSmilieSmilie
# 2  
Old 07-24-2009
to give you a head start
Code:
-bash-3.2$ date +"%Y%m%d"
20090724
-bash-3.2$ date +"%m%d%y"
072409
-bash-3.2$

# 3  
Old 07-24-2009
hi ryanthegr8,

I cannot use DATE command . I have to extract the date out of the filenames like
071609 and change it to 20090716. Change is not a problem but I need 20090716 in number format. Does it make sense?sorry again if im not clear. And thanks though
# 4  
Old 07-24-2009
try this
Code:
-bash-3.2$ file='filenameAGHYSW_fg_050509.tar.gz'
-bash-3.2$ filedate=`echo ${file//[^0-9]/}`
-bash-3.2$ year=`echo $filedate | sed 's|^....||'`
-bash-3.2$ monthday=`echo $filedate |sed 's|..$||g'`
-bash-3.2$ timestamp=`echo "20${year}${monthday}"`
-bash-3.2$ echo $file | sed "s|$filedate|$timestamp|"
filenameAGHYSW_fg_20090505.tar.gz
-bash-3.2$

# 5  
Old 07-24-2009
Hi RubinPat

you can try this one

Code:
filename=filenameA_fg_072409.tar.gz
fileformatdate=`echo $filename | cut -d "_" -f3 | cut -d "." -f1`
year=20`echo $fileformatdate | cut -c 5-6`
mmday=`echo $fileformatdate | cut -c 1-4`
reqformateddate=`echo $year$mmday`

# 6  
Old 07-24-2009
Code:
#!/bin/ksh
myowndate="$1"
shift
for fname in $*
do
    # remove all before last _ including _
    step1=${fname##*_}
    # remove all after first . including .
    mmddyy=${step1%%.*}
    yyyymmdd="20${mmddyy:4:2}${mmddyy:0:2}${mmddyy:2:2}"
    if (( yyyymmdd < myowndate )) ; then
        echo "$fname"
    fi
done

using ex.
Code:
./thisscript 20081130 filename*_*tar*



---------- Post updated at 10:41 AM ---------- Previous update was at 10:35 AM ----------

your "about" scriptlines include some syntax error.
No space using set value for variable:
formatdate=`echo ...

Using variable, need $
Code:
if [ "$formatdate" -lt "$myowndate" ] ; then
   ...
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to compare two dates in a specific format ?

Hello, I am not able to find how to compare two dates in '%Y-%m-%d %H:%M:%S,%3N' format i have to two dates as below date1="2016-08-24 09:47:40,444" date2="2016-08-24 10:45:40,567" how to compare these two dates ? I have tried below without success if ; then echo 'yes'; fi ... (6 Replies)
Discussion started by: Ramneekgupta91
6 Replies

2. Shell Programming and Scripting

Change just the date format

need some more help please i have a large file and i want to change just the date format with awk or sed from this yyyy-mm-dd 2016-04-15 8.30 2016-04-15 7.30 2016-04-13 7.30 2016-04-11 8.30 2016-04-11 7.30 2016-04-08 8.30 2016-04-08 7.30 2016-04-06... (5 Replies)
Discussion started by: bob123
5 Replies

3. Shell Programming and Scripting

Compare Date with String in date format

Hi, I am new to this forum, searched, but for some reason did not find much help, so a new thread. I am trying to compare date with a string which is in date format , but for some reason, system does not give me the right result. Date is coming in a file and i am comparing the same with... (2 Replies)
Discussion started by: mkstool
2 Replies

4. Shell Programming and Scripting

ksh compare dates INSIDE a file (ie date A is > date B)

In KSH, I am pasting 2 almost identical files together and each one has a date and time on each line. I need to determine if the first instance of the date/time is greater than the 2nd instance of the date/time. If the first instance is greater, I just need to echo that line. I thought I would... (4 Replies)
Discussion started by: right_coaster
4 Replies

5. Shell Programming and Scripting

awk compare column with date format

I have this code to compare columns 1 and 10 between file1 and file 2 and give me all records that match column 1 but dont match column 10 However column 10 is date format mm/dd/yy and awk cant read it and compare ...i tried awk < file1 -F~ '{print $10}' and it gave blank screen Is... (1 Reply)
Discussion started by: sigh2010
1 Replies

6. UNIX and Linux Applications

sqlite: calculating with dates - compare current date minus 6 months with stored record

Hi I have a table with name, date in format DD.MM.YYYY. I need to something like this (I try to explain in pseudo code) if SYSDATE (current date) minus 6 months > $expiry date print OK else print NOK with $name and $expiry date I know this is possible with Oracle. How to do this... (0 Replies)
Discussion started by: slashdotweenie
0 Replies

7. Solaris

Date after 5 dates in YYYYMMDD format

Hi Experts, How to get date 5 days after current date in YYYYMMDD format? How do we compare date in YYYYMMDD format? Thanks (1 Reply)
Discussion started by: needyourhelp10
1 Replies

8. Shell Programming and Scripting

Date difference between 2 dates in 'yyyy-mm-dd hh:mm:ss' format

Hi all, I know this may have already been asked but hey ho... i have two dates in the 'yyyy-mm-dd hh:mm:ss' format. '2009-01-03 01:00:00' '2009-04-05 00:00:00' How can i, in shell script determine their differences? Please note, the time may not be available, so please suggest both... (4 Replies)
Discussion started by: muay_tb
4 Replies

9. Shell Programming and Scripting

How to Change the Format of a Date

Hi All, this is my second post, last post reply was very helpful. I have a data that has date in DD/MM/YYYY (07/11/2008) format i want to replace the backslash by a dot(.) so that my awk script can read it inside the C shell script that i have written. i want to change 07/11/2008 to... (3 Replies)
Discussion started by: asirohi
3 Replies

10. Shell Programming and Scripting

Compare dates in a field and print the latest date row

Hi, I need a shell script which should find the latest date in the field of file and print that line only. For eg., I have a file /date.log Name Date Status IBM 06/06/07 close DELL 07/27/07 open DELL 06/07/07 open : : : From... (1 Reply)
Discussion started by: cvkishore
1 Replies
Login or Register to Ask a Question