将管道中的项目导入到数组中 [英] Import the items from the pipeline in to an array

查看:57
本文介绍了将管道中的项目导入到数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有一列信息的文本文件,该信息是通过CSV导入的。

I have a text file that has one column of info, which was imported via CSV.

#TODO: Initialize Form Controls here         
$Date = (Get-Date -Format "MM-dd-yyyy")      
$DateTime = (Get-Date)                       
$Projects = Import-Csv c:\temp\pm_project.csv
#Show me the entire list of Projects         
$Projects




number   
------   
FOAH18   
FOAH278  
FOAH313  
PRJ0031905  
PRJ0031909  
PRJ0032045  

这些是在下拉列表中选择的,并放置在文本文件中。他们还有其他选择,例如管理时间或其他非项目时间。

These are chosen in a dropdown and placed in a text file. There are other choices they can make like administrative or other non project time.

$Matches = Get-Content "c:\temp\timesheet\$Date.txt" 
#Show me all the entries in the text file            
$Matches                                             




11/06/2017 18:45:56 - This 15 minutes is dedicated to PROJECT ID FOAH18  
11/06/2017 18:45:58 - This 15 minutes is dedicated to PROJECT ID FOAH278   
11/06/2017 18:45:59 - This 15 minutes is dedicated to PROJECT ID FOAH313  
11/06/2017 18:46:14 - This 15 minutes is dedicated to Administrative functions  
11/06/2017 18:46:18 - This 15 minutes is dedicated to Sustain functions  
11/06/2017 18:46:18 - This 15 minutes is dedicated to Sustain functions   
11/06/2017 18:45:56 - This 15 minutes is dedicated to PROJECT ID PRJ0031905  
11/06/2017 18:45:56 - This 15 minutes is dedicated to PROJECT ID PRJ0031909  
11/06/2017 18:45:56 - This 15 minutes is dedicated to PROJECT ID PRJ0032045  

我有一个regex命令,可以将包含项目ID号的行拉出

I have a regex command that pulls the lines out that contain the Project ID number, which produces the below in to the pipeline.

#Show me only the lines in the test file that match the regex statement                                  
$Matches = Select-String -Path "C:\temp\TimeSheet\$Date.txt" -Pattern '(?>FOAH|PRJ)\d{1,10}' -AllMatches 
$Matches  
$Matches.Count # This does show the proper amount of lines that match the regex command.




C:\temp\TimeSheet\11-06-2017.txt:1:11/06/2017 18:45:56 - This 15 minutes is dedicated to PROJECT ID FOAH18
C:\temp\TimeSheet\11-06-2017.txt:2:11/06/2017 18:45:58 - This 15 minutes is dedicated to PROJECT ID FOAH278  
C:\temp\TimeSheet\11-06-2017.txt:3:11/06/2017 18:45:59 - This 15 minutes is dedicated to PROJECT ID FOAH313  
C:\temp\TimeSheet\11-06-2017.txt:4:11/06/2017 18:46:00 - This 15 minutes is dedicated to PROJECT ID PRJ0031905  
C:\temp\TimeSheet\11-06-2017.txt:5:11/06/2017 18:46:02 - This 15 minutes is dedicated to PROJECT ID PRJ0031909  
C:\temp\TimeSheet\11-06-2017.txt:6:11/06/2017 18:46:03 - This 15 minutes is dedicated to PROJECT ID PRJ0032045 

我确实从管道中导出了数据,并在下面得到了结果,但是我不知道为什么我要获得整行而不是正则表达式过滤器

I did export the data from the pipeline and got the results below, but I don't know why I am getting the entire line instead of the regex filter.


Line  
11/06/2017 18:45:56 - This 15 minutes is dedicated to PROJECT ID FOAH652  
11/06/2017 18:45:58 - This 15 minutes is dedicated to PROJECT ID FOAH705  
11/06/2017 18:45:59 - This 15 minutes is dedicated to PROJECT ID FOAH721  
11/06/2017 18:46:00 - This 15 minutes is dedicated to PROJECT ID FOAH780  
11/06/2017 18:46:02 - This 15 minutes is dedicated to PROJECT ID FOAH787  
11/06/2017 18:46:03 - This 15 minutes is dedicated to PROJECT ID FOAH787 

这是我被困在那里。我的目标如下:

This is where I am stuck. My goals are as follows:


  1. 仅将所有正则表达式命中的项目号放入数组中。我只需要FOAH或PRJ号。没什么。

  1. Put the project number only from all the regex hits in to an array. I only need the FOAH or PRJ number. Nothing else.

获取该列表中每个列表的数量。他们将全天为同一项目输入多个条目。

Get a count of how many of each are in this list. They will have several entries of the same projects throughout the day.

获取每个项目的总工作时间。每次输入都是15分钟。

Get the total amount of minutes worked on each project. Each entry is 15 minutes.

将此数据导出到CSV文件中以进行进一步处理。

Export this data in to a CSV file for further manipulation.


推荐答案

选择字符串产生 MatchInfo 对象,默认情况下PowerShell会显示 Line 属性( Filename 处理文件时使用LineNumber Line 属性)。要只获取匹配项,您需要扩展 Matches 属性,然后扩展每个匹配项的 Value 属性:

Select-String produces MatchInfo objects, of which PowerShell by default displays the Line property (Filename, LineNumber, and Line properties when processing files). To get just the matches you need to expand the Matches property, and then the Value property of each match:

Select-String ... |
    Select-Object -Expand Matches |
    Select-Object -Expand Value

此外, $ matches 自动变量。请勿为其分配值。

Also, $matches is an automatic variable. Do not assign values to it.

这篇关于将管道中的项目导入到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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