Import the items from the pipeline in to an array

人盡茶涼 提交于 2019-12-24 23:54:15

问题


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  

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. Put the project number only from all the regex hits in to an array. I only need the FOAH or PRJ number. Nothing else.

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

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

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


回答1:


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

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



来源:https://stackoverflow.com/questions/47148500/import-the-items-from-the-pipeline-in-to-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!