awk - change date format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - change date format
# 1  
Old 01-24-2013
IBM awk - change date format

I have below date format in a CSV file. (dd/mm/yyyy)
Ex Input:
9/8/2013

Need to convert it into below format (yyyymmdd ) and redirect to new file.
Ex Output:
20130809

How do I use awk here to change the format and if leading 0 (zero) is not then add it.
Please help. Thanks.
# 2  
Old 01-24-2013
What is the format of the csv file?
# 3  
Old 01-24-2013
Check this one
Code:
echo 9/8/2013 | awk -F/ '{printf "%s%02s%02s\n", $3, $2, $1}'

This User Gave Thanks to posix For This Post:
# 4  
Old 01-25-2013
Quote:
Originally Posted by posix
Check this one
Code:
echo 9/8/2013 | awk -F/ '{printf "%s%02s%02s\n", $3, $2, $1}'

Worked! thanks a lot Smilie

---------- Post updated at 01:15 AM ---------- Previous update was at 12:35 AM ----------

I am not able to use awk for this, can anyone please help?

input csv file record (there will be 50k records in one file)

number, number, date time, number
123456789,98765432,9/8/2012 3:00,1

Need output:
date , time , number , number, number
20130809,030000,123456789,98765432,1

Need to convert date and time and put it in first position. How do i do it? Please advise. Thanks.
# 5  
Old 01-25-2013
Code:
awk -F'[,| |/|:]' 'NR>1{printf "%s%02s%02s,%02s%s00,%d,%d,%d\n",$5,$4,$3,$6,$7,$1,$2,$NF; }' file.csv

This User Gave Thanks to Yoda For This Post:
# 6  
Old 01-25-2013
Thanks for your reply.... this is working for the data I gave... but in actual file i also have strings with spaces and it could have : / as well.... but file is comma separated.

eg:
Input file:
Code:
number,      number,   date time,  number, string, string
123456789,98765432,9/8/2012 3:00,1, ABC ABC, NEW STRING (12/3)

Need output:
Code:
   date , time ,            number ,    number,   number, string, string
20130809,030000,123456789,98765432,1, ABC ABC, NEW STRING (1:23)

Please advise. Sorry for not giving complete data before!

Last edited by Scrutinizer; 01-25-2013 at 03:51 AM.. Reason: code tags
# 7  
Old 01-25-2013
Quote:
Originally Posted by vegasluxor
Thanks for your reply.... this is working for the data I gave... but in actual file i also have strings with spaces and it could have : / as well.... but file is comma separated.

eg:
Input file:
number, number, date time, number, string, string
123456789,98765432,9/8/2012 3:00,1, ABC ABC, NEW STRING (12/3)

Need output:
date , time , number , number, number, string, string
20130809,030000,123456789,98765432,1, ABC ABC, NEW STRING (1:23)

Please advise. Sorry for not giving complete data before!
So is the transformation of xy/z to x:yz supposed to occur in both string fields or only in the last field? Will that transformation always be for 2 digits followed by a slash followed by a single digit, or is there some more general pattern?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Date format conversion how to change this from using nawk to awk

Hi, I have a file where I need to change the date format on the nth field from DD-MM-YYYY to YYYY-MM-DD so I can accurately sort the record by dates From regex - Use sed or awk to fix date format - Stack Overflow, I found an example using nawk. Test run as below: $: cat xyz.txt A ... (2 Replies)
Discussion started by: newbie_01
2 Replies

2. Shell Programming and Scripting

Need to change date format in a csv file using awk

Example: Input csv file 00245DLS,Sitel Ocala,12/31/2014,18:45,1.00,7.00,0.00,0.00 00245DLS,Sitel Ocala,12/31/2014,19:00,-1.00,-1.00,-1.00,-1.00 00245HB,Charlotte,01/01/2015,00:00,-1.00,-1.00,-1.00,0.00 Output csv file 00245DLS,Sitel Ocala,2014/12/31,18:45,1.00,7.00,0.00,0.00 00245DLS,Sitel... (8 Replies)
Discussion started by: adit
8 Replies

3. Shell Programming and Scripting

Change the date format

Hi, I was looking for a script to change the date from one format to other. A search in the forum gave me the below script as a result. #! /bin/ksh format=YYYYMMDD YEAR=${format%????} DAY=${format#??????} MON=${format#$YEAR} MON=${MON%$DAY} echo $MON/$DAY/$YEAR I got it... (2 Replies)
Discussion started by: prithvirao17
2 Replies

4. 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

5. Shell Programming and Scripting

Date format change

Dear Friends, Need your help once again, I have a variable ( e.g. ${i}) whoch has date in MM/DD/YYYY (E.g. 12/31/2011) format. I want to change it to DD/MM/YYYY (e.g. 31/12/2011) format. Request you to guide me as we are unable to do the same. Thanks in advance Anu. (1 Reply)
Discussion started by: anushree.a
1 Replies

6. Shell Programming and Scripting

How to change the date format

Hi Guys, Can someone help me on how to change the date format using sed or any unix command to give my desired output as shown below. INPUT FILE: 69372,200,20100122T17:56:02,2 53329,500,20100121T11:50:07,2 48865,100,20100114T16:08:16,2 11719,200,20100108T13:32:20,2 DESIRED... (2 Replies)
Discussion started by: pinpe
2 Replies

7. Shell Programming and Scripting

Date Format Change

Hi, Please can I have some command to get yesterday in YYMMDD format? This will be used in ksh Thanks, (2 Replies)
Discussion started by: smalya
2 Replies

8. 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

9. 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

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