Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

solve the read date problem

Status
Not open for further replies.

hlyeoh

MIS
May 31, 2004
22
MY
i have the following script to read the date for a file :

- get_mdate.awk

{
timestamp = substr($0, 42, 12)
month = substr(timestamp, 1, 3)
day = substr(timestamp, 5, 2)

thisyear = substr(todaydate, 1, 4)
thismonth = substr(todaydate, 5, 2)

if (month == "Jan") {
monthnum = "01"
} else if (month == "Feb") {
monthnum = "02"
} else if (month == "Feb") {
monthnum = "02"
} else if (month == "Mar") {
monthnum = "03"
} else if (month == "Apr") {
monthnum = "04"
} else if (month == "May") {
monthnum = "05"
} else if (month == "Jun") {
monthnum = "06"
} else if (month == "Jul") {
monthnum = "07"
} else if (month == "Aug") {
monthnum = "08"
} else if (month == "Sep") {
monthnum = "09"
} else if (month == "Oct") {
monthnum = "10"
} else if (month == "Nov") {
monthnum = "11"
} else if (month == "Dec") {
monthnum = "12"
}

year = substr(timestamp, 9, 4)
timeseparator = substr(timestamp, 10, 1)
if (timeseparator == ":") {
if (monthnum > thismonth) {
year = thisyear - 1
} else {
year = thisyear
}
}

printf("%04d%02d%02d\n",year,monthnum,day)
}

i will read it by
ls -tl {path_t-_read _file} | awk -f {path to read get_mdate.awk} todaydate=$todaydate

but the problems happen when the file size is too big, and the date will be shift a bit and end up read wrongly. example, as following,(the agebklog.dat) :

-rw-rw---- 1 jbdaemon bsp 999620 Jul 21 01:34 GROSS_BILLINGS.200110
-rw-rw---- 1 jbdaemon bsp 940966 Jul 21 01:34 GROSS_BILLINGS.200111
-rw-r----- 1 jbdaemon bsp 27411492 Jul 21 14:46 agebklog.dat

is there another solution to solve this? tq
 
You may consider perl:
file="/path/to/file"
eval `perl -e '
($ss,$mn,$hh,$dd,$mm,$yy,$wd,$yd,$dst)=localtime((stat("'$file'"))[9]);
printf("DATE=%04d%02d%02d\n",$yy+1900,$mm+1,$dd);
printf("TIME=%02d%02d%02d\n",$hh,$mn,$ss)'`
echo "$file:"$DATE,$TIME

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
You seem to be using strings and sub-strings that rely on columns being in fixed character positions. How about using awk to "chop up" the line. Column 6 is the Month, column 7 is the Date and column 8 is the Time or the Year (depending on whether the file was modified in the last 6 months) regardless of the width of the "file size" field.

I hope that helps you.

Mike
 
thanx for everyone .. it works for me now =)
 
the result is as below :

-rwxrwxrwx 1 devtest bsp 9 Aug 2 04:05 GROSS_BILLINGS_last_mdate.tok

but i just need to grep the date,and not the full path. The date should return me the value 20040809 .. anyway, thanx for the help. =)
 
oh!
On my system (linux) it gives
-rwx... 1 devtest bsp 2002-08-09 04:05 GROSS_BI...
which would have made the rest of the work more easy - at least shorter and independent of a Locale.

But if 'ls' differs so much over systems, you would only change one dependency against another one.

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top