将数组中的一行拆分为4个不同的值

时间:2017-11-08 01:58:08

标签: arrays powershell

如果您希望完整地分析脚本以及我目前获得的结果,您可以参考我以前的帖子。

Import the items from the pipeline in to an array

我试图弄清楚如何只从阵列中获取我需要的两个字段,这样我就可以操纵数据了。现在,数组将整行显示为数组中的第一个元素。

$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
$TS_Entries = Get-Content "c:\temp\timesheet\$Date.txt"
#Show me all the chosen entries in the text file
$TS_Entries
#Show me only the lines in the text file that match the regex statement
$TS_Entries = Select-String -Path "C:\temp\TimeSheet\$Date.txt" -Pattern '(?>FOAH|PRJ)\d{1,10}' -allmatches | Select-Object -expand matches | Select-Object -expand Value
$TS_Entries
$TS_Entries | group -NoElement 

我似乎无法利用数组中的count元素。我需要在计数中获取每个值的值,并将它们乘以15.我在下面的列表中唯一可以参考的是名称

Count Name   
----- ----   
    2 FOAH278
    1 FOAH519
    1 FOAH704
    3 FOAH718
    2 FOAH780

2 个答案:

答案 0 :(得分:0)

所以我不确定我完全明白你在问什么,但这里有:

所以说你有一个txtfile:

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

以下脚本使用.NET regex类

$test = gc "C:\temp\stack.txt" 
$m = [regex]::Matches($test,'(?msi)((?<=Project )\d{0,}).*?(FOAH|PRJ)(\d{1,10})') 

正则表达式示例:https://regex101.com/r/yhgl9v/1

foreach($match in $m){
    write-host "ID:" $match.Groups[1].Value 
    write-host "Prefix:" $match.Groups[2].Value 
    write-host "Number:" $match.Groups[3].Value 
    ""
}

这会给你:

ID: 100
Prefix: FOAH
Number: 18

ID: 90
Prefix: FOAH
Number: 278

ID: 80
Prefix: FOAH
Number: 313

ID: 70
Prefix: PRJ
Number: 0031905

ID: 60
Prefix: PRJ
Number: 0031909

ID: 50
Prefix: PRJ
Number: 0032045

这是你要找的吗?

答案 1 :(得分:0)

$test = gc "C:\temp\stack.txt" 
$m = [regex]::Matches($test,'(?msi)((?<=Project )\d{0,}).*?(FOAH|PRJ)(\d{1,10})') 
$arr=@()
foreach($match in $m){

   $arr += [PSCustomObject]@{  
    id = $match.Groups[1].Value 
    prefix = $match.Groups[2].Value 
    number = $match.Groups[3].Value 
   }
}

$arr | Export-Csv C:\temp\stock.csv -NoTypeInformation -Delimiter ';'

因此,您创建了一个与值索引系统一起使用的customobject(哈希表)。 一旦你填满了你的阵列,你可以稍后(在循环之外)将其导出到csv文件中。 您的CSV文件将包含:

"id";"prefix";"number"
"100";"FOAH";"18"
"90";"FOAH";"278"
....