converting date format: "May 31 2008" to "2008-05-31"


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers converting date format: "May 31 2008" to "2008-05-31"
# 1  
Old 06-17-2008
converting date format: "May 31 2008" to "2008-05-31"

I have the following script to find out the last day of the last month .... and the output of this script is in the following format ...

Script goes like this ....

#!/bin/ksh
cur_month=`date +%m`
cur_year=`date +%Y`
prev_month=$(($cur_month-1))

# Check to see if this is January
if [ $prev_month -lt 1 ]
then
prev_year=$(($cur_year-1))
prev_month=12
LastDayOfLastMonth=`/bin/cal $prev_month $cur_year | grep -v "^$" | sed -n '1p;$p' | tr "\n" " " | awk '{print substr($1,1,3),$N
F,$2}'`
else
LastDayOfLastMonth=`/bin/cal $prev_month $cur_year | grep -v "^$" | sed -n '1p;$p' | tr "\n" " " | awk '{print substr($1,1,3),$N
F,$2}'`
fi
echo $LastDayOfLastMonth

Output will be like this ...
May 31 2008


I want this output to be converted to the following format and displayed ...
2008-05-31 -- This is the format i need the output to be

Please help me with this .... Thx
# 2  
Old 06-17-2008
man date....

# date '+DATE: %Y-%m-%d'
DATE: 2008-06-17
# 3  
Old 06-17-2008
is there a way we can get the output of the above script in the following format ..... like ... yyyy-mm-dd

the above mentioned script gives me output as " May 31 2008"
# 4  
Old 06-17-2008
???
Watch:
on a IBM AIX:
vbe@ ello9: /home/vbe# uname
AIX
vbe@ ello9: /home/vbe# oslevel
5.3.0.0
vbe@ ello9: /home/vbe# date '+DATE: %Y-%m-%d'
DATE: 2008-06-17
vbe@ ello9: /home/vbe#

Now SUN
olf:/export/home/vbe # more /etc/release
Solaris 9 12/02 s9s_u2wos_10a SPARC
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved.
Use is subject to license terms.
Assembled 02 December 2002
olf:/export/home/vbe # date '+DATE: %Y-%m-%d'
DATE: 2008-06-17
olf:/export/home/vbe #

HP HPUX?
ant:/home/vbe $ uname -a
HP-UX ant B.11.11 U 9000/800 16..1511 unlimited-user license
ant:/home/vbe $ date '+DATE: %Y-%m-%d'
DATE: 2008-06-17
ant:/home/vbe $

READ the man pages!!!
# 5  
Old 06-17-2008
Thanks vbe ...
I think my question was bit confusing ... let me tell you what exactly i need ..

I have provided a script in my 1st post ... in that script .... i'm just selecting particular peices of information from $cal (calender) to get the last day of last month ....
and here ... i'm picking up month name, last date and year using grep, sed, tr, awk .... displaying it in the format "May 31 2008" (this just a string of data) ... and i want this output string (that is $LastDayOfMonth in my script) to be converted to a specific format (that is yyyy-mm-dd) ...
# 6  
Old 06-17-2008
Did you have a look at perderabo's script?
https://www.unix.com/unix-dummies-que...html#post16559
# 7  
Old 06-17-2008
first day and last day of previous month

even though his script is a good one ... but it is not of much help to me with the present issue I have ...
it would be gr8 if someone can help me get the first day of last month and last day of last month in yyyy-mm-dd format.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. AIX

xx=`date +"%a %b %d"`;rsh xxx grep "^$XX" zzz ?

AIX 4.2 I am trying to do an rsh grep to search for date records inside server logs by doing this : xx=`date +"%a %b %d"` rsh xxx grep "^$XX" zzz gives : grep: 0652-033 Cannot open Jun. grep: 0652-033 Cannot open 11. But if I do : xx=`date +"%a %b %d"` grep "^$XX" zzz it works... (2 Replies)
Discussion started by: Browser_ice
2 Replies

7. UNIX for Advanced & Expert Users

add seconds to: date"|"time"|"HHMMSS

Hey all, I have a shell that invokes a AWK. In this AWK i want invoke a function that receives 3 parameters: date: 20080831 time: 235901 duration: 00023 that function receive this 3 parameters and sum to this value two more seconds: 2008083123590100025 Remember that in case that... (3 Replies)
Discussion started by: anaconga
3 Replies

8. Shell Programming and Scripting

sed remove date ex. "Mar 25 2008"

is there any way to remove data out of a file with sed? sample file: 2 3 414 Mar 25 2008 223 312 4244 Feb 25 2008 5 312 422344 Sept 25 2008 output: 2 3 414 223 312 4244 5 312 422344 (14 Replies)
Discussion started by: katrvu
14 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question