use shell to iterate through a file

后端 未结 2 697
北恋
北恋 2021-01-27 08:29

I have two input files. One has namelist, mm:dd form, duration of usage and other host name stuff in each line. The other one is the one that I generated that has a set of namel

2条回答
  •  一整个雨季
    2021-01-27 08:38

    I solved this using perl through this code from what I understood from you question.

    #!/usr/bin/perl -w
    use strict;
    use warnings;
    
    use Data::Dumper;   # $fields[3] => Month $fields[0] => user $fields[8] => Time
    
    my $user_time = {};
    my $time = {};
    open my $READ , '<', 'test' or die;
    
    while(my $line = <$READ>){
        my @fields = split(' ', $line);
    
        my $user = $fields[0];
        my $month = $fields[3];
    
        $fields[8] =~ m/([\d]*)\+?([\d]{2}):([\d]{2})/; #time format
        my $min = $3;
        my $hr = $2;
        my $day = $1;
        $day = 0 if (!$day);
        if (!exists $user_time->{$month}->{$user}){
            $time = {};
        }
        $time->{'day'} += $day*24;
        $time->{'hr'} += $hr;
        $time->{'min'} += $min;
    
        $user_time->{$month}->{$user} = $time;
    }
    close $READ;
    foreach my $month (keys %$user_time){
        print "[$month]\n";
        my $user_hash = $user_time->{$month};
        foreach my $user (keys %$user_hash){
            my $time = $user_hash->{$user};
            print "$user\t". $time->{'day'}.'.'.$time->{'hr'}.'.'.$time->{'min'}."hours\n";
        }
    }
    

    INPUT FILE:

    sdou     pts/11       Thu Dec 10 05:24 - 12:11 (2+06:46)    131.243.186.99
    sdou     pts/10       Thu Dec 10 05:04 - 12:11 (2+07:06)    131.243.186.99
    sdou     pts/9        Thu Dec 10 03:26 - 12:11 (2+08:45)    131.243.186.99
    ermartin pts/0        Sat Dec 12 12:37 - 13:44  (01:06)     c-24-130-14-154.hsd1.ca.comcast.net
    ermartin pts/0        Sat Dec 12 12:18 - 12:31  (00:13)     c-24-130-14-154.hsd1.ca.comcast.net
    

    OUTPUT:

    [Dec]
    ermartin 0.1.19hours
    sdou     144.21.97hours
    

    Hope this helps.

提交回复
热议问题