converting date format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting converting date format
# 1  
Old 05-21-2008
converting date format

Hi

Please anyone to help to write script to convert date to 'yyyy-mm-dd'(If column is date convert) because my file has large and lot of date colums.

If file is small ,using awk command to achive.

cat file1|awk 'BEGIN {FS=OFS='|'} {$1=substr($1,1,4)"-"substr($1,5,2)"-"substr($1,7,2);{$2=substr($2,1,4)"-"substr($2,5,2)"-"substr($2,7,2);print $0}'



Thanks,
MR
# 2  
Old 05-21-2008
Please provide an example of part of your file.
# 3  
Old 05-21-2008
Hi,

Sample data
99990101|20080331|0.0000000|FBF7.375 12/09|0.0000000|20070809|20080908|0.0000000|12/09|.....


Thanks
MR
# 4  
Old 05-22-2008
Quote:
Originally Posted by mohan705
cat file1|awk 'BEGIN {FS=OFS='|'} {$1=substr($1,1,4)"-"substr($1,5,2)"-"substr($1,7,2);{$2=substr($2,1,4)"-"substr($2,5,2)"-"substr($2,7,2);print $0}'
This should give you a boost.
Code:
awk 'BEGIN {FS=OFS="|"} {for (i=1;i<=NF;i++) if (length($i)==8) $i=substr($i,1,4)"-"substr($i,5,2)"-"substr($i,7,2);print }' file1


Last edited by danmero; 05-22-2008 at 01:24 AM..
# 5  
Old 05-22-2008
Hi danmero,

Thanks ,This is what I am looking for.

Thanks,
MR
# 6  
Old 05-22-2008
Hi danmero

How it can be handled If any othey colum that having length of 8 other than date columns ? (Is there any command to validate the date ).In that case it will take the columns data and convert to date format

Thanks,
MR
# 7  
Old 05-22-2008
Yes, you can validate the date if you know the date range, here is a basic example:
Code:
awk 'BEGIN {FS=OFS="|"} {for (i=1;i<=NF;i++) if ($i ~ /[1-2][0-9][0-9][0-9][0-1][0-9][0-3][0-9]/) $i=substr($i,1,4)"-"substr($i,5,2)"-"substr($i,7,2);print }' file1

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - problems by converting date-format

Hi i try to change the date-format from DD/MM/YYYY into MM/DD/YY. Input-Data: ... 31/12/2013,23:40,198.00,6.20,2,2,2,1,11580.0,222 31/12/2013,23:50,209.00,7.30,2,2,3,0,4380.0 01/01/2014,00:00,205.90,8.30,2,2,3,1,9360.0,223 ... Output-Data should be: ...... (7 Replies)
Discussion started by: IMPe
7 Replies

2. UNIX for Dummies Questions & Answers

Rename all Files in a UNIX Directory from one date format to another date format

Hi Unix Gurus, I would like to rename several files in a Unix Directory . The filenames can have more than 1 underscore ( _ ) and the last underscore is always followed by a date in the format mmddyyyy. The Extension of the files can be .txt or .pdf or .xls etc and is case insensitive ie... (1 Reply)
Discussion started by: pchegoor
1 Replies

3. Shell Programming and Scripting

Converting a date to friday date and finding Min/Max date

Dear all, I have 2 questions. I have a file with many rows which has date of the format YYYYMMDD. 1. I need to change the date to that weeks friday date(Ex: 20120716(monday) to 20120720). Satuday/Sunday has to be changed to next week friday date too. 2. After converting the date to... (10 Replies)
Discussion started by: 2001.arun
10 Replies

4. Shell Programming and Scripting

Converting the date format

Hi All, I am new to this forum. Could anyone help me to resolve the following issue. Input of the flat file contains several lines of text for example find below: 5022090,2,4,7154,88,,,,,4/1/2011 0:00,Z,L,2 5022090,3,1,6648,88,,,,,4/1/2011 0:00,Z,,1 5022090,4,1,6648,88,,,,,4/1/2011... (6 Replies)
Discussion started by: av_sagar
6 Replies

5. UNIX for Advanced & Expert Users

Converting the date format in a flat file

Hi All, I am new to this forum, could any one help me out in resolving the below issue. Input of the flat file contains several lines of text for example find below: 5022090,2,4,7154,88,,,,,4/1/2011 0:00,Z,L,2 5022090,3,1,6648,88,,,,,4/1/2011 0:00,Z,,1... (0 Replies)
Discussion started by: av_sagar
0 Replies

6. Shell Programming and Scripting

converting Date format

Dear Friends, We have date in output as MM/DD/YYYY format and I want to convert it in DD/MM/YYYY format. e.g. 12/31/2010 to 31/12/2010 We have checked forum for the solution but we are not getting matching query. Please guide us Thanks Anushree. (6 Replies)
Discussion started by: anushree.a
6 Replies

7. Shell Programming and Scripting

Converting windows format file to unix format using script

Hi, I am having couple of files which i used to copy from windows to Linux, so now in case of text files (CTRL^M) appears at end of line. I know i can convert this windows format file to unix format file by running dos2unix. My requirement here is that i want to do it automatically using a... (5 Replies)
Discussion started by: sarbjit
5 Replies

8. Shell Programming and Scripting

convert date format to mysql date format in log file

I have a comma delimited log file which has the date as MM/DD/YY in the 2nd column, and HH:MM:SS in the 3rd column. I need to change the date format to YYYY-MM-DD and merge it with the the time HH:MM:SS. How will I got about this? Sample input 02/27/09,23:52:31 02/27/09,23:52:52... (3 Replies)
Discussion started by: hazno
3 Replies

9. UNIX for Dummies Questions & Answers

Converting the File Creation Date to a new format

I need to capture a file's creation/modification date and time and convert this to a different format, whilst I can easily get the existing format from a ls -l | awk ' { print $......}' or a cut command I do not know how to convert it to a desired format? I should add that at present the ls -l... (1 Reply)
Discussion started by: barney_clough
1 Replies
Login or Register to Ask a Question