use shell to iterate through a file

后端 未结 2 696
北恋
北恋 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:46

    I thought this was what you wanted (uses GNU awk 4.* for true multi-dimensional arrays):

    $ cat tst.awk
    {
        n = split($9,t,/[()+:]/)
        hours = t[n-3]*24 + t[n-2] + t[n-1]/60
        tot[$4][$1] += hours
    }
    END {
        for (month in tot) {
            print "["month"]"
            for (user in tot[month]) {
                print user, tot[month][user] "hours"
            }
        }
    }
    
    $ awk -f tst.awk file
    [Dec]
    sdou 166.617hours
    ermartin 1.31667hours
    

    but the output numbers don't match your expected values:

    [Dec]
    sdou 94.xxxhours
    ermartin 1.19hours
    

    I've spent a lot of time trying to figure out why they're different but I can't. Sorry, hope this helps anyway.

提交回复
热议问题