The UNIX and Linux Forums  

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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Time Between Dates Sreejith_VK HP-UX 2 02-26-2008 11:02 PM
Comparing two dates guptan Shell Programming and Scripting 5 11-25-2005 08:46 AM
Searching between dates Khoomfire Shell Programming and Scripting 9 08-22-2005 08:11 AM
compare two dates wchen Shell Programming and Scripting 6 10-24-2002 01:39 PM
Formatting dates in a file maverick UNIX for Dummies Questions & Answers 6 12-18-2001 08:46 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 02-28-2006
Registered User
 

Join Date: Aug 2005
Posts: 111
Question formatting dates

How do I insert a string into a particular character position using SED,AWK or KSH?

I have a text file with contents (dates) below and I want to change the format from YYYYMMDD to YYYY/MM/DD.

Code:
$ more input_date
20060227
20051201
20040130
I created a shell below which does the conversion but is there an easier way to do this?

Code:
$ more format_date.ksh
#!/bin/ksh

while read line
do

echo $line > tmp$$

YEAR=$(sed 's/....$//g' tmp$$)
DAY=$(sed 's/^......//g' tmp$$)
MONTH=$(sed -e 's/^....//g' -e 's/..$//g' tmp$$)

rm tmp$$

echo "$YEAR/$MONTH/$DAY"

done < $1
Code:
$ format_date.ksh input_date
2006/02/27
2005/12/01
2004/01/30
Reply With Quote
Forum Sponsor
  #2  
Old 02-28-2006
Registered User
 

Join Date: Dec 2005
Location: India
Posts: 218
try using the cut command with -c option
Reply With Quote
  #3  
Old 02-28-2006
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
Code:
sed 's/\(....\)\(..\)\(..\)/\1\/\2\/\3/' filename
Reply With Quote
  #4  
Old 02-28-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,698
Using shell builtin's

Code:
#!/bin/ksh

while read line
do

YEAR=${line%????}
day_mon=${line#$YEAR}
MON=${day_mon%??}
DAY=${day_mon#??}

echo "$YEAR/$MON/$DAY"

done < input
Reply With Quote
  #5  
Old 02-28-2006
Registered User
 

Join Date: Aug 2005
Posts: 111
Smile

Thanks guys!
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:55 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0