The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
a simple way of converting a date in seconds to normal date travian HP-UX 2 11-23-2006 12:25 PM
converting text to csv format gthokala Shell Programming and Scripting 13 06-09-2006 10:44 AM
converting PDF to text, rtf doc format saurya_s UNIX for Advanced & Expert Users 1 04-23-2004 03:25 PM
Converting BMP to BM (or other unix format) EJ =) UNIX Desktop for Dummies Questions & Answers 1 06-12-2002 08:42 AM
Converting the File Creation Date to a new format barney_clough UNIX for Dummies Questions & Answers 1 06-12-2002 07:43 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-21-2008
mohan705 mohan705 is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 134
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 (permalink)  
Old 05-21-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,912
Please provide an example of part of your file.
  #3 (permalink)  
Old 05-21-2008
mohan705 mohan705 is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 134
Hi,

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


Thanks
MR
  #4 (permalink)  
Old 05-21-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,419
Quote:
Originally Posted by mohan705 View Post
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 12:24 AM..
  #5 (permalink)  
Old 05-22-2008
mohan705 mohan705 is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 134
Hi danmero,

Thanks ,This is what I am looking for.

Thanks,
MR
  #6 (permalink)  
Old 05-22-2008
mohan705 mohan705 is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 134
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 (permalink)  
Old 05-22-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,419
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
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:02 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0