Operating System - HP-UX
1827808 Members
2424 Online
109969 Solutions
New Discussion

Pulling and formatting dates of a file (ls)

 
Matt_330
New Member

Pulling and formatting dates of a file (ls)

I would like to be able to format the create date / touch date of a file from 'ls'.

something that will let me change the format to MM/DD/YYYY (no timestamp needed for my purpose).

I have awk yanking the date out of the ls, but cannot seem to reformat in a nice way without doing lookup conversions.. I was hoping for something more elegant than that.

TIA!
4 REPLIES 4
Alan Meyer_4
Respected Contributor

Re: Pulling and formatting dates of a file (ls)

this is the only way I can think... probably more lookup conversions that you want though...

ll $1 |awk '{print $6,$7,$8}' |read MN DY YR
case $MN in
Jan) MN="01";;
Feb) MN="02";;
Mar) MN="03";;
Apr) MN="04";;
May) MN="05";;
Jun) MN="06";;
Jul) MN="07";;
Aug) MN="08";;
Sep) MN="09";;
Oct) MN="10";;
Nov) MN="11";;
Dec) MN-"12";;
esac

[ $DY -lt 10 ] && DY="0$DY"

[ -n "`echo $YR |grep ':'`" ] && YR=`date '+%Y'`
[ $MN -gt `date '+%M'` ] && YR=`eval $YR - 1`

print "$MN/$DY/$YR"
" I may not be certified, but I am certifiable... "
Tim Nelson
Honored Contributor

Re: Pulling and formatting dates of a file (ls)

A silly simple solution is that only the MM needs to be fixed.
Use awk, separate the MM(text) DD and YYY from ls. Then take the MM(text) and use a conversion table, case statement, or other means to convert the text month to a number.

i.e
case MM in
Jan) MONTH=1
Feb) Month=2
...;;
esac

Alan Meyer_4
Respected Contributor

Re: Pulling and formatting dates of a file (ls)

oops, should be this...

ll $1 |awk '{print $6,$7,$8}' |read MN DY YR
case $MN in
Jan) MN="01";;
Feb) MN="02";;
Mar) MN="03";;
Apr) MN="04";;
May) MN="05";;
Jun) MN="06";;
Jul) MN="07";;
Aug) MN="08";;
Sep) MN="09";;
Oct) MN="10";;
Nov) MN="11";;
Dec) MN-"12";;
esac

[ $DY -lt 10 ] && DY="0$DY"

[ -n "`echo $YR |grep ':'`" ] && YR=`date '+%Y'`
[ $MN -gt `date '+%M'` ] && YR=`eval $YR - 1`

print "$MN/$DY/$YR"
"fordate.sh" 25 lines, 439 characters
[ $DY -lt 10 ] && DY="0$DY"

if [ -n "`echo $YR |grep ':'`" ] ;then
YR=`date '+%Y'`
[ $MN -gt `date '+%M'` ] && YR=`eval $YR - 1`
fi

print "$MN/$DY/$YR"
" I may not be certified, but I am certifiable... "
Rodney Hills
Honored Contributor

Re: Pulling and formatting dates of a file (ls)

Rather then using 'ls', which varies the format if the date is over 6 months, you could use 'els'. See-

http://hpux.cs.utah.edu/hppd/hpux/Users/els-1.48a/

HTH

-- Rod Hills
There be dragons...