使用shell通过文件迭代 [英] use shell to iterate through a file

查看:162
本文介绍了使用shell通过文件迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个输入文件。一个人NAMELIST,MM:DD形式,每条线的使用率等主机名的东西时间。另一种是,我生成具有在每一个不重复线上的一组名称列表中的一个。
我想写一个shell命令通过与名称列表文件进行迭代,将它们存储为变量,与其他文件检查相匹配的名称和使用的每个人,然后输出总的持续时间,然后输出包含文件和信息。反正我有可以用awk或者什么做的工作?

我想通了命令:
在$字(猫名称列表);做回声$字; DONE
但是这仅是为了通过一个文件迭代。
我想要做的是这样的:

 在namelist.txt字:
 在info_file.txt字(场$ 1):
   如果Word ==话:
     (计算时使用)
     打印(文字,':',时间使用)

我已经想通了codeS来计算时间的使用情况。这只是如何做到这一点的操作,是我的问题现在。
有谁请帮助吗?

-----------------进一步解释
因此,我已一个文件含有用户消耗登录到每一行具有名称的信息,日期主机的时间信息(毫米:DD:HH:MM),初始时间,结束时间,持续时间和主机名。
我想实现的是输出每个月的文件作为标题(例如:【4月]),然后计算出谁登录到主机的每个用户的持续时间。他们可能会多登录所以他们的名字出现在各行中的其它信息多次。我已经生成此文件具有已在过去半年已经登陆的所有用户名。我试图来生成一个月,每个月名称输出文件的标题,组,然后与我产生的得到他们的持续时间计算并打印到输出文件在同一行的名称列表文件中匹配的名字他们的名字印。我已经想通了如何做计算。所以现在它只是我该怎么办名称匹配的步骤混淆我

输入文件:

  SDOU分/ 11周四12月10日05:24  -  12:11(2 + 06:46)131.243.186.99
SDOU PTS / 10周四12月10日05:04 - 12:11(2 + 07:06)131.243.186.99
SDOU PTS / 9星期四12月10日03:26 - 12:11(2 + 08:45)131.243.186.99
ermartin PTS / 0周六12月12日12:37 - 13:44(01:06)c-24-130-14-154.hsd1.ca.comcast.net
ermartin PTS / 0周六12月12日12:18 - 12:31(00:13)c-24-130-14-154.hsd1.ca.comcast.net

输出示例:

  [月]
SDOU 94.xxxhours
ermartin1.19​​小时


解决方案

我想这是你想要的(使用了GNU AWK 4. *真正的多维数组):

  $猫tst.awk
{
    N =拆分($ 9日,T,/ [()+:] /)
    小时= T [N-3] * 24 + T [N-2] + T [N-1] / 60
    TOT [$ 4] [$ 1] + =小时
}
结束 {
    对于(TOT中的一个月){
        打印[月]
        对于(TOT中的[月]的用户){
            打印用户,TOT [月] [用户]时间
        }
    }
}$ AWK -f tst.awk文件
[月]
SDOU166.617小时
ermartin1.31667小时

但输出的数字不符合您的预期值:

  [月]
SDOU 94.xxxhours
ermartin1.19​​小时

我花了很多时间试图弄清楚为什么他们是不同的,但我不能。对不起,希望这有助于反正。

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 namelist in each line that does not repeat. I am trying to write a shell command to iterate through the file with the namelist, store them as a variable and check with the other file to match the name and then output the total duration of usage for each person and then output a file containing and information. Is there anyway I can use awk or anything to do the work?

I figured the command: for word in $(cat namelist); do echo $word; done but this is only for iterating through one file. What I wanna do is something like this:

for word in namelist.txt:
 for words in info_file.txt (field $1):
   if word == words:
     (calculating usage of time) 
     print(word, ':', usage of time)

I have already figured out the codes to calculate the usage of time. It's just how to do this operation that is my problem right now. Could anybody please help?

-----------------further explanation So I have one file containing information of the time that the user consumed to login into the host that each row has the information of name, date(mm:dd:hh:mm), initial time, end time, time duration and host name. What I want to achieve is to output a file with each Month as a title(for example: [April]) and then calculate the time duration of each user who login into the host. They might have multi logins so their names appear multiple times with the other informations in each rows. I have generated this file that has all the user names that has been login during past half a year. I am trying to generate output file with each Month Name as a title, and group by month and then match the names in the file with the namelist that I generated to get their time duration calculated and printed into the output file on the same row with their name printed. I have already figured out how to do the calculations. So right now it's just how do I do the steps of matching names that confuses me

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

sample output:

[Dec]
sdou 94.xxxhours
ermartin 1.19hours

解决方案

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.

这篇关于使用shell通过文件迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆