Solved

retrive date in unix

Posted on 2014-04-26
2
374 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: 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

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

726 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