[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Unix Korn Shell - Converting Strings to Dates

Posted on 2006-04-13
5
Medium Priority
?
893 Views
Last Modified: 2008-01-09
I am pulling the Day,  Month , Year from various files using 'ls -l' to allow me to compare files in one directory against another.  The month is coming out as 'Oct'.  The problem is I need to convert my final string e.g. '10/May/2005' to a date format to allow me to filter which has the latest date etc.  I have trawled various sites with no answers.

I have used awk to extract the positions from ls-l process.   Any help appreciated....
0
Comment
Question by:btcodeman
5 Comments
 
LVL 21

Expert Comment

by:oleggold
ID: 16447303
You can use awk printf command to do that
0
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 16450973
echo '10/May/2005' | awk -F/ 'BEGIN{m["Jan"]=1;;m["Mar"]=3;m["Apr"]=4;m["May"]=5;m["Jun"]=6;m["Jul"]=7;m["Aug"]=8;m["Sep"]=9;m["Oct"]=10;m["Nov"]=11;m["Dec"]=12}{printf "%4d-%02d-%-2d",$3,m[$2],$1}'
0
 
LVL 85

Expert Comment

by:ozo
ID: 16450977
echo '10/May/2005' | awk -F/ 'BEGIN{m["Jan"]=1;;m["Mar"]=3;m["Apr"]=4;m["May"]=5;m["Jun"]=6;m["Jul"]=7;m["Aug"]=8;m["Sep"]=9;m["Oct"]=10;m["Nov"]=11;m["Dec"]=12}{printf "%4d-%02d-%02d",$3,m[$2],$1}'
0
 
LVL 4

Expert Comment

by:NVRAM
ID: 16458947
You might step back and ask if you can get 'ls' to give what you want.  If you are using Linux, or GNU 'ls', you could try reformatting the output of 'ls' instead, as with:

    ls -l --time-style='+%Y/%m/%d-%M:%H:%S'

Which will give dates listed like this:    2006/03/17-10:06:03
0
 

Author Comment

by:btcodeman
ID: 16475681
Ozo,  thanks for the answer  - much appreciated.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Simple Linear Regression
Six Sigma Control Plans

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question