Using back references in SED

Today I have several CSV files, everyone with a header at the first line and a date at the first fields with the format mm/dd/yyyy. It was necessary to change that field into the format yyyy-mm-dd.

Here there is a Perl solution and also a SED one:

#!/usr/bin/perl


use strict;
use warnings;


while (<>) {


        chomp;


        if (/^[^0-9]/) {
                print «$_\n»;
                next;
        }


        my @Fields = split /,/, $_, 2;


        my ($Month, $Day, $Year) = split /\//, $Fields[0];


        print «$Year-$Month-$Day, $Fields[1]\n»;


}


__END__


sed -i.bak ‘s/^\([0-9]\+\)\/\([0-9]\+\)\/\([0-9]\+\)/\3-\1-\2/’ *.csv

Deja un comentario

Tu dirección de correo electrónico no será publicada.