Search code examples
linuxperlhadoopooziefalcon

Update date and time in text file via Linux script or falcon : Hadoop


I have some text file with below entries :

Name  type  startTime   Endtime    comments
my   I      01-03-2016  02-03-2016 zoom
my   F     01-03-2016  02-03-2016  zoom2
abd   F     03-03-2016  04-03-2016  zoom5
my   I      01-03-2016  02-03-2016 zoom6 

If the Currnt date is march 18 : the output should be : Output :

Name  type  startTime   Endtime    comments
my   I      **02-03-2016**  ***18-03-2016*** zoom
my   F     01-03-2016  02-03-2016  zoom2
abd   F     03-03-2016  04-03-2016  zoom5
my   I      **02-03-2016**  ***18-03-2016*** zoom6 

Conditions are If name == my && type ==I then needs to update the start time with End time -- End time would be current date which is processed :

Can any one help me in choosing best methodology to process that file with above requirements .

I hope my requirement is cleared :) Thanks, Madhu


Solution

  • Pure Perl solution will look like

    #!/usr/bin/env perl
    
    use strict;
    use warnings;
    
    open(my $fh, "<", "file.txt") || die $!;
    my ($header, @lines) = <$fh>;
    close($fh);
    
    my @keys = split(/[\s\t]+/, $header);
    
    open($fh, ">", "file.txt") || die $!;
    print $fh join("\t",@keys), "\n";
    
    my @cdate = (localtime)[3,4,5];
    $cdate[1] += 1;
    $cdate[2] += 1900;
    
    foreach my $line (@lines) {
        my %tmp;
        @tmp{@keys} = split(/[\s\t]+/, $line);
        if($tmp{'Name'} eq 'my' && $tmp{'type'} eq 'I') {
            $tmp{'Endtime'} = sprintf("%02d-%02d-%04d", @cdate)
        }
    
        print $fh join("\t", @tmp{@keys} ),"\n"
    }
    
    close($fh)