【问题标题】:Splitting a line in an array in to 4 different values将数组中的一行拆分为 4 个不同的值
【发布时间】:2017-11-08 01:58:08
【问题描述】:

如果您想要完整的脚本分解以及我目前获得的结果,可以参考我之前的帖子。

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。我可以在下面的列表中引用的唯一内容是 Name

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

【问题讨论】:

  • 试试Flatten-Object: $TS_Final |展平对象
  • 能否请您提供一个示例文本行?这样会更容易提供帮助。
  • @Snak3d0c 我已经放了一个链接到我的另一篇文章,该文章显示了从脚本到问题区域的结果。 stackoverflow.com/questions/47148500/…
  • @Snak3d0c 在导出到 csv 文件之前,我已经更新了这篇文章底部的结果。希望这会有所帮助。

标签: arrays powershell


【解决方案1】:

所以我不确定我是否完全理解你的要求,但这里是:

假设你有一个 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 正则表达式类

$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

这是你要找的吗?

【讨论】:

  • 目前一切都很完美。我在这篇文章的最后一个块中有我需要的两个值。我似乎无法将该数据导出到 csv 文件。
【解决方案2】:
$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 ';'

因此,您创建了一个与值索引系统一起使用的自定义对象(哈希表)。 填满数组后,您可以稍后(在循环外)将其导出到 csv 文件中。 您的 CSV 文件将包含:

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

【讨论】:

  • 顺便说一句, ?msi 应该是 ?mi ,不需要 S。
猜你喜欢
  • 1970-01-01
  • 2019-03-16
  • 1970-01-01
  • 2012-05-08
  • 1970-01-01
  • 2018-06-17
  • 2022-11-23
  • 1970-01-01
  • 2018-01-09
相关资源
最近更新 更多