Solved

retrive date in unix

Posted on 2014-04-26
2
360 Views
Last Modified: 2014-04-30
I need to know if there is a function in unix where I pass a number and in return should get the month as a output  like if I pass 01  the return should be jan and 02 it should be feb etc
Is there any already built in function or any simple user defined function I can use etc?
0
Comment
Question by:welcome 123
  • 2
2 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 40024988
If you have GNU date just run

date -d "01/01" "+%b"

The first "01" is the one which counts, the remainder ("01" in my example) can be freely chosen between "01" and "28".

Without GNU date you'll need a small script to achieve what you desire:

 echo "01" |awk '{split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec",A," "); print A[$1+0]}'
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40025187
A bash/ksh function embedded in an example script:
#!/bin/bash
function convmon {
 if RESULT=$(date -d "$1/01" "+%b" 2>/dev/null)
    then echo $RESULT
     else
       RESULT=$(awk -v N=$1 '
                BEGIN {split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec",A," "); print A[+N+0]}
                            ')
         echo $RESULT
 fi
}

if [ $1 -gt 0 -a $1 -le 12 ]
   then NUMMON=$1
    else
     echo "Invalid Argument $1. Allowed 01..12." >&2; exit 22
fi
CHARMON=$(convmon $NUMMON)
echo $CHARMON

Open in new window

It will work with or without GNU date being available.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

759 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

20 Experts available now in Live!

Get 1:1 Help Now