Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

change yyyy-mm-dd to ddmmyy

Status
Not open for further replies.

hcclnoodles

IS-IT--Management
Jun 3, 2004
123
GB
Hi all

Wonder if you can help , I have a comma delimited file where the 13th to the 31st character of each line is populated by the date and time."2004-07-21 09:39:06"

16-5-141551,2004-07-21 09:39:06,8815
22-5-340394,2004-07-22 09:39:41,8001
22-5-340433,2004-07-23 09:42:20,1701

I have been asked to remove the time completely (i can probably work that bit out for myself) and reformat the date to DDMMYY so effectivly the file should look like this

16-5-141551,210704,8815
22-5-340394,220704,8001
22-5-340433,230704,1701

I really havent got a clue how to get started on this, hence the post, any help would be greatly appreciated

Gary
 
The awk way:
awk -F',' '
{printf "%s,%s%s%s,%s\n",$1,substr($2,9,2),substr($2,6,2),substr($2,1,4),$3}
' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Assuming your line format really is standard a simple, brute force version would be
Code:
while read part1 part2
do
part1_1=`echo $part1 | cut -d ',' -f 1`
year=`echo $part1 | cut -c15,16`
month=`echo $part1 | cut -c18,19`
day=`echo $part1 | cut -c21,22`
part2_2=`echo $part2 | cut -d',' -f 2`
echo "$part1_1$year$month$day,$part2_2"
done
I'm sure there's someone out there who will have an elegant version using awk but this one is easy to maintain
 
And PHV's solution was elegant and maintainable!
 
One way using perl:
Code:
#!/usr/bin/perl -w
while (<>) {
    chomp;
    @fields = split(',');
    if ($fields[1] =~ /^\d{2}(\d{2})-(\d{2})-(\d{2})/) {
        print "$fields[0],$3$2$1,$fields[2]\n";
    }
}
Cheers, Neil
 
or command line perl:

Code:
perl -pe 's/,\d\d(\d\d)-(\d\d)-(\d\d) \d\d:\d\d:\d\d,/,$3$2$1,/' inputfile

Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L

 
PHV's solution was elegant and maintainable!
NO, NO, NO
PHV's solution was not elegant
PHV's solution is elegant

... he just make a lot of assumption, uses MAGICS,
substr($2,9,2),substr($2,6,2), ...
and this sure works!

i prefer stefan's && rod's solutions...
both need little work to become more generalized
:)
 
Code:
awk '
  BEGIN { FS=OFS="," }
  {
    $2 = substr($2,9,2) substr($2,6,2) substr($2,3,2)
    print $0
  }
' infile > outfile
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top