Unix Korn Shell - Converting Strings to Dates

Posted on 2006-04-13
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....
Question by:btcodeman
    LVL 21

    Expert Comment

    You can use awk printf command to do that
    LVL 84

    Accepted Solution

    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}'
    LVL 84

    Expert Comment

    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}'
    LVL 4

    Expert Comment

    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

    Author Comment

    Ozo,  thanks for the answer  - much appreciated.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Report events not being triggered 8 71
    Sql how to exclude from a table 3 61
    bigHeights  challenge 13 43
    wordmultiple challenge 12 67
    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now