script to change the date format in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to change the date format in a file
# 8  
Old 06-10-2008
Change the awk delimiter:

Code:
awk -F"|" .....

It seems that your date field is the 7th, so you need to change $3 with $7 in the script.

BTW, I assume the third record you provided is in wrong format!
# 9  
Old 06-10-2008
Quote:
Originally Posted by robotronic
Change the awk delimiter:

Code:
awk -F"|" .....

It seems that your date field is the 7th, so you need to change $3 with $7 in the script.

BTW, I assume the third record you provided is in wrong format!
How about if i wanna do both $7 and $8?
# 10  
Old 06-10-2008
Quote:
Originally Posted by shehzad_m
also couple files like these

12|kenneth.ludlam@yahoo.com|Boston|iVPUd|MPDD|2-10-2007|2-11-2007|
12|kenneth.ludlam@yahoo.com|Boston|iVPUd|MPDD|12-9-2007|12-9-2007|
12|kenneth.ludlam@yahoo.com|Boston|iVPUd|MPDD|2-2-2007|2-2-2007|
The method is always the same, just add another array variable to keep the second date you need to manipulate, eg:

Code:
awk -F"|" '{
   split($6, d1, "-");
   split($7, d2, "-");
   $6=sprintf("%04d-%02d-%02d", d1[3], d1[1], d1[2]);
   $7=sprintf("%04d-%02d-%02d", d2[3], d2[1], d2[2]);
   print;
}' input_file.txt

# 11  
Old 06-10-2008
Quote:
Originally Posted by robotronic
The method is always the same, just add another array variable to keep the second date you need to manipulate, eg:

Code:
awk -F"|" '{
   split($6, d1, "-");
   split($7, d2, "-");
   $6=sprintf("%04d-%02d-%02d", d1[3], d1[1], d1[2]);
   $7=sprintf("%04d-%02d-%02d", d2[3], d2[1], d2[2]);
   print;
}' input_file.txt

Thanks works great.
# 12  
Old 06-10-2008
Quote:
Originally Posted by robotronic
The method is always the same, just add another array variable to keep the second date you need to manipulate, eg:

Code:
awk -F"|" '{
   split($6, d1, "-");
   split($7, d2, "-");
   $6=sprintf("%04d-%02d-%02d", d1[3], d1[1], d1[2]);
   $7=sprintf("%04d-%02d-%02d", d2[3], d2[1], d2[2]);
   print;
}' input_file.txt


By the way can i keep the pipe delimted? it removes all the pipes.
# 13  
Old 06-10-2008
Oh, sure Smilie

Just add the line:
Code:
OFS="|";

before split commands.
# 14  
Old 06-10-2008
Quote:
Originally Posted by robotronic
Oh, sure Smilie

Just add the line:
Code:
OFS="|";

before split commands.
I am getting a too long error after record 83 on my file when i run awk...is there a fix for this ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Change date format in a file.

Hi all, I have a file as below, i would like the change the format of the time from "11/7/2019 20:12" to "2019-07-11 20:12:00" in the last coloumn. any awk solution on this. Input: 2,0,695016,1961612,497212,5800804,0,0,161,33,7605,12226,23,10,66,0,0,34,11/7/2019 20:10... (4 Replies)
Discussion started by: Raghuram717
4 Replies

2. Shell Programming and Scripting

Date format change in a csv file

Hi, We have csv file where date is coming in MM/DD/YYYY HH:MM:SS (06/23/2015 20:59:12) in multiple places But we need to change the date format to DD/Mon/YYYY HH:MM:SS (23/Jul/2015 20:59:12) using shell script. Please let us know how can we achieve the same. (16 Replies)
Discussion started by: dholea
16 Replies

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

4. Shell Programming and Scripting

Date format change in UNIX .dat file

Hi, I need help to convert the date format in .DAT file in unix. I want to convert 10@@|SWIFT MT568 Extract@@|Apr 14 2014 5:47:52:563PM@@|Apr 14 2014 4:33:47:663PM@@||##| into 10@@|SWIFT MT568 Extract@@|04/14/2014/ 5:47:52:563PM@@|04/14/2014 4:33:47:663PM@@||##| Appreciate... (18 Replies)
Discussion started by: karthikengox
18 Replies

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

6. Shell Programming and Scripting

How to change the format of the date column in a flat file?

Hi, i have a flat file namely temp.txt with this data below ID|name|contact_date 101|Kay|2013-12-26 102|let|2013-12-26 I need to modify the date data in the flat file into MM/DD/YYYY HH24:MI:SS format let me know the code for this. Thank you! (5 Replies)
Discussion started by: srikanth_sagi
5 Replies

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

8. Shell Programming and Scripting

File date format how to change

Hi All, Below are the unix files taken by the help of ls -lrt -rw-r--r-- 1 kbehera Domain Users 293 Jul 27 13:33 sand.txt -rw-r--r-- 1 kbehera Domain Users 4 Jul 27 13:37 sand1.txt -rw-r--r-- 1 kbehera Domain Users 293 Jul 27 15:30 new_sand.txt -rw-r--r-- 1 kbehera Domain Users 0 Jul 27... (2 Replies)
Discussion started by: krupasindhu18
2 Replies

9. Shell Programming and Scripting

How to change date format in file

Hello! I have a textfile that look like this: "83d1:46:2b";"20091008190000";"Rögle BK - Skellefteå";"Swedish" "d4c:46:21";"20091008190000";"Södertälje - Brynäs";"Swedish" "d4b:46:2";"20091008190000";"HV 71 - Färjestad";"Swedish" "838:46:b";"20091010160000";"Skellefteå - HV 71";"Swedish"... (2 Replies)
Discussion started by: condmaster
2 Replies

10. Shell Programming and Scripting

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 ? (4 Replies)
Discussion started by: pccwtest
4 Replies
Login or Register to Ask a Question