计算相同ID的值

时间:2017-03-22 05:48:15

标签: powershell

我是Powershell的新手,我正在尝试自动执行我经常做的繁琐工作,如果你能够帮助我或者如何实现我的目标,我会非常感激。

基本上我有一个类似这样的CSV文件:

Name   | Message | Date     | Time 
------ | ------  | ------   | -----
John   | Good    | 1/1/2001 | 1:00 PM
John   | Bad     | 1/2/2002 | 2:00 AM
Frank  | Better  | 1/3/2003 | 3:00 PM
Frank  | Best    | 1/4/2004 | 4:00 AM
Mike   | Great   | 1/5/2005 | 5:00 PM
Mike   | Perfect | 1/6/2006 | 6:00 AM
Mike   | Awesome | 1/7/2007 | 7:00 PM

我想要实现的输出看起来像这样:

Name   | Message_Count
------ | ------
John   | 2  
Frank  | 2 
Mike   | 3

到目前为止,我只能删除重复项,但我无法弄清楚如何计算相应名称上的消息。

3 个答案:

答案 0 :(得分:2)

此代码可以为您提供帮助。

2017-03-22 06:20:59,184 :[2017-03-22 06:20:59,182] ERROR in app: Exception on / [POST]

2017-03-22 06:20:59,186 :Traceback (most recent call last):
2017-03-22 06:20:59,186 :  File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
2017-03-22 06:20:59,186 :    response = self.full_dispatch_request()
2017-03-22 06:20:59,187 :  File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
2017-03-22 06:20:59,187 :    rv = self.handle_user_exception(e)
2017-03-22 06:20:59,187 :  File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
2017-03-22 06:20:59,187 :    reraise(exc_type, exc_value, tb)
2017-03-22 06:20:59,188 :  File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
2017-03-22 06:20:59,188 :    rv = self.dispatch_request()
2017-03-22 06:20:59,188 :  File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
2017-03-22 06:20:59,188 :    return self.view_functions[rule.endpoint](**req.view_args)
2017-03-22 06:20:59,188 :  File "/home/Firiyuu77/mysite/flask_app.py", line 64, in index
2017-03-22 06:20:59,188 :    result = forecast(rem,sold1,sold2)
2017-03-22 06:20:59,188 :  File "/home/Firiyuu77/mysite/flask_app.py", line 26, in forecast
2017-03-22 06:20:59,189 :    if((rem/3)>=(sold1+10) or (rem/3)>=(sold1+10)):
2017-03-22 06:20:59,189 :TypeError: unsupported operand type(s) for /: 'unicode' and 'int'

答案 1 :(得分:1)

在Snippet下面也会给出相同的结果

import-csv 'Input Path' | Group-Object Name | Select-Object Name,count

答案 2 :(得分:0)

假设输入文件正是您提供的方式,那么我们需要做一些工作,将其置于PowerShell可识别为CSV的格式中。我已经采用了mfatih的代码来做到这一点:

Get-Content file.txt |
    % { $_ -replace "\s*\|\s*","|" } |  # Get rid of the spaces around the fields
    ConvertFrom-Csv -Delimiter '|' |    # Use "|" as the delimiter.
    ? Name -notmatch '^-+$' |           # Skip the "------" row.
    Group-Object Name | 
    Select-Object @{Name='Name'; Expression={$_.Values[0]}}, 
                  @{Name='Message_Count'; Expression={$_.Count}}

输出:

Name  Message_Count
----  -------------
John              2
Frank             2
Mike              3