help for change date format script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help for change date format script
# 1  
Old 02-17-2009
Bug help for change date format script

Someone can help me write a script for change date fromat
from "Feb 4 18:44:03 2009" to 2009020418 ?
from "Mar 17 16:44:03 2009" to 2009031716 ?
# 2  
Old 02-17-2009
Seems easier than it looks, here's one approach with awk:

Code:
awk -F" |:" '
BEGIN {
  a="Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
  split(a,m,",")
  for(i=1;i<13;i++)
    n[m[i]]=i
}
{printf("%s%02d%02d%s\n", $6, n[$1], $2, $3)}
'

Output:

Code:
$
$ echo "Feb 4 18:44:03 2009"|
awk -F" |:" '
BEGIN {
  a="Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
  split(a,m,",")
  for(i=1;i<13;i++)
    n[m[i]]=i
}
{printf("%s%02d%02d%s\n", $6, n[$1], $2, $3)}
'
2009020418
$
$

Use nawk or /usr/xpg4/bin/awk on Solaris.


Regards
This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 02-18-2009
Quote:
Originally Posted by Franklin52
Seems easier than it looks, here's one approach with awk:

Code:
awk -F" |:" '
BEGIN {
  a="Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
  split(a,m,",")
  for(i=1;i<13;i++)
    n[m]=i
}
{printf("%s%02d%02d%s\n", $6, n[$1], $2, $3)}
'

Output:

Code:
$

Code:
$ echo "Feb 4 18:44:03 2009"|
awk -F" |:" '
BEGIN {
a="Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
split(a,m,",")
for(i=1;i<13;i++)
   n[m]=i
}
{printf("%s%02d%02d%s\n", $6, n[$1], $2, $3)}
'
2009020418
$
[i][i]$

Use nawk or /usr/xpg4/bin/awk on Solaris.


Regards

Thanks for your help but i got the result is 2009020418:44:02 not 2009020418

awk -F" :" '
BEGIN {
a="Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
split(a,m,",")
for(i=1;i<13;i++)
n[m[i]]=i
}
{printf("%s%02d%02d%s\n",$4,n[$1],$2,$3)}



Kindly please help. Thank you
# 4  
Old 02-19-2009
You have some typos in your code, compare your code with the code of my first post.

Regards
# 5  
Old 02-20-2009
Hi,

if you get above details using date command in your unix..
then hope this also might help you


command:

date -d "Feb 4 18:44:03 2009" +"%Y%m%d%H"
date -d "Mar 17 16:44:03 2009" +"%Y%m%d%H"

output:
2009020418
2009031716

Thanks
Sha
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change just the date format

need some more help please i have a large file and i want to change just the date format with awk or sed from this yyyy-mm-dd 2016-04-15 8.30 2016-04-15 7.30 2016-04-13 7.30 2016-04-11 8.30 2016-04-11 7.30 2016-04-08 8.30 2016-04-08 7.30 2016-04-06... (5 Replies)
Discussion started by: bob123
5 Replies

2. Shell Programming and Scripting

Change the date and time format in UNIX script.

Hi, I am extracting a date string from the source file like this : 06/05/2014 16:04:00 I want to change it to 05-JUN-14 04.05.00.000000000 PM I basically store the date in a variable. I got solutions to change date in dd-mmm-yyyy format using tr but I guess it works only with the "date"... (8 Replies)
Discussion started by: Varshha
8 Replies

3. UNIX for Dummies Questions & Answers

Change Date from one format to other

Hi I wish to change date from one format to another in unix. eg: INPUT DATE: 2013159 (YEAR & NUMBER OF DAY) OUTPUT DATE required: 20130608 (YYYYMMDD) how to do it ? Thanks in advance. (6 Replies)
Discussion started by: dashing201
6 Replies

4. Shell Programming and Scripting

Change date format in shell script

Plz help me To display date in the mm/dd/yyyy. Eg. if date is 28-09-2012 the output of the shell script should be date 09/28/2012. (1 Reply)
Discussion started by: shivasaini
1 Replies

5. Shell Programming and Scripting

Change the date format

Hi all, I have a file that every line starts with the date and time. The format is like YYYYMMDDHHMM and I woulk like to change it to MM/DD/YY<space>HH:MM. I tried to figure out a way to do it with sed, but I don't know how I could reorganize the digits of the first format. Does anyone have any... (1 Reply)
Discussion started by: geovas
1 Replies

6. Shell Programming and Scripting

Change Date Format

Hi Guys, I had a scenario like this.. It seems very silly...dont think it as a home work question.....:) i tried it many ways but i didn't achieve this... start_date=May122011 here i want to change the start_date in to 20110512 start_date=20110512 tell me how can we achive... (5 Replies)
Discussion started by: apple2685
5 Replies

7. Shell Programming and Scripting

Date Format Change

Hi I have a date format in a variable as Mon Jan 11 03:35:59 2010. how do i change it to YYYYMMDD format (3 Replies)
Discussion started by: keerthan
3 Replies

8. Shell Programming and Scripting

How to Change the Format of a Date

Hi All, this is my second post, last post reply was very helpful. I have a data that has date in DD/MM/YYYY (07/11/2008) format i want to replace the backslash by a dot(.) so that my awk script can read it inside the C shell script that i have written. i want to change 07/11/2008 to... (3 Replies)
Discussion started by: asirohi
3 Replies

9. Shell Programming and Scripting

script to change the date format in a file

i have many files with date format of 6-9-2008 and i want a script that can change the format to 2008-06-09 Thanks (15 Replies)
Discussion started by: shehzad_m
15 Replies

10. Shell Programming and Scripting

Change of date format

I want to chnage the date format from the file format like below to WT;T15D;0000007208;;20080401;3;0;0;3;;B;ZZZZZZ; WT;T25D;0000007208;;20080401;6;0;0;6;;B;ZZZZZZ; WT;T5D;0000007208;;20080401;123;0;0;123;;B;ZZZZZZ; to WT;T15D;0000007208;;04/01/200804;3;0;0;3;;B;ZZZZZZ;... (2 Replies)
Discussion started by: svenkatareddy
2 Replies
Login or Register to Ask a Question