[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

retrive date in unix

Posted on 2014-04-26
2
Medium Priority
?
390 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 2000 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

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
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.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

649 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