Solved

retrive date in unix

Posted on 2014-04-26
2
371 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Linux Scripting 3 144
Creating 2 files from output with Powershell 5 55
Identify two keyboard codes and how to enter them 9 56
LINUX Field Separators 7 35
Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

856 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