如何将批量文本文件的 select-string 输出保存到数组 powershell
How to save select-string output from bulk text files to an array powershell
我正在尝试从文本文件中批量提取子字符串,并将这些子字符串保存到一个数组中。我尝试了以下变体。这会将所有选定的字符串输出到屏幕,但仅将最终输出保存到变量。有没有办法在 outvariable 中模仿 =+ 运算符的功能,以便所有项目都存储在数组中?
$FILES = ls "*.txt"
foreach($f in $FILES){
$in=Get-Content $f
$in | Foreach { Select-String -Path "$f" -Pattern "Ad ID" -outvariable
array1 }}
如果我的策略被误导,将子字符串拉入数组的总体目的是让这些文本文件的单独子字符串组成多个数组。然后我会将这些值连接成一个 csv。我试图拉出元素而不是重新排列文本文件,因为文本文件中的子字符串顺序不同。示例:
txt文件一:
Ad Id: xxxx
Ad Text: blah blah
Ad placement: spaceship
txt 文件二:
Ad Id: yyyy
Ad placement: zoo
Ad Text: blah blah
最终期望的结果(除了元素的顺序,这部分是有效的)
CSV 文件
xxxx, spaceship, blah blah
yyyy, zoo, blah blah
这是一种构建您所说的数组的方法。我认为这不是解决此问题的最佳方法。这不会影响结果的顺序,也不会创建 .csv 文件。
$FILES = Get-ChildItem -File -Filter "*.txt"
$array1 = $()
foreach($f in $FILES) {
Get-Content -Path $f |
Select-String -Pattern "Ad Id.*" |
ForEach-Object { $array1 += @($_.Matches.Value) }
}
$FILES.Count
$array1.Count
$array1
试试这个:
$files = ls "*.txt"
$dictionary = @{}
foreach($f in $files) {
$in = Get-Content $f
$in.Split([Environment]::NewLine) | ForEach-Object {
$key,$value = $_.Split(':')
$dictionary[$key] = $value
}
$dictionary['Ad Id'] + ', ' + $dictionary['Ad placement'] + ', ' + $dictionary['Ad Text'] | Out-File -FilePath '.\results.csv' -Append
}
排序输出:
$files = ls "fil*.txt"
$dictionary = @{}
[System.Collections.Generic.List[String]]$list = @()
foreach($f in $files) {
$in = Get-Content $f
$in.Split([Environment]::NewLine) | ForEach-Object {
$key,$value = $_.Split(':')
$dictionary[$key] = $value
}
[void]$list.Add( $dictionary['Ad Id'] + ', ' + $dictionary['Ad placement'] + ', ' + $dictionary['Ad Text'] )
}
[void]$list.Sort()
$list | Out-File -FilePath '.\results.csv' -Append
另一种略有不同的方法。
- RegEx 解析 $Line 并创建一个变量,其名称在冒号前(不带
Ad
),值在冒号后
- 在每个处理过的文件之后,变量都作为自定义对象输出
$Data = ForEach ($File in (Get-ChildItem File*.txt)){
$Id,$Text,$Placement="","",""
ForEach ($Line in (Get-Content $File)){
If ($Line -Match "AD (?<Label>.*?): (?<Value>.*)"){
Set-Variable -Name "$($Matches.Label)" -Value $Matches.Value
}
}
[PSCustomObject]@{ID = $Id
Placement = $placement
Text = $Text}
}
$Data
$Data | Export-CSv ".\Result.csv" -NoTypeInformation
示例输出:
ID Placement Text
-- --------- ----
xxxx spaceship blah blah
yyyy zoo blah blah
我正在尝试从文本文件中批量提取子字符串,并将这些子字符串保存到一个数组中。我尝试了以下变体。这会将所有选定的字符串输出到屏幕,但仅将最终输出保存到变量。有没有办法在 outvariable 中模仿 =+ 运算符的功能,以便所有项目都存储在数组中?
$FILES = ls "*.txt"
foreach($f in $FILES){
$in=Get-Content $f
$in | Foreach { Select-String -Path "$f" -Pattern "Ad ID" -outvariable
array1 }}
如果我的策略被误导,将子字符串拉入数组的总体目的是让这些文本文件的单独子字符串组成多个数组。然后我会将这些值连接成一个 csv。我试图拉出元素而不是重新排列文本文件,因为文本文件中的子字符串顺序不同。示例:
txt文件一:
Ad Id: xxxx
Ad Text: blah blah
Ad placement: spaceship
txt 文件二:
Ad Id: yyyy
Ad placement: zoo
Ad Text: blah blah
最终期望的结果(除了元素的顺序,这部分是有效的)
CSV 文件
xxxx, spaceship, blah blah
yyyy, zoo, blah blah
这是一种构建您所说的数组的方法。我认为这不是解决此问题的最佳方法。这不会影响结果的顺序,也不会创建 .csv 文件。
$FILES = Get-ChildItem -File -Filter "*.txt"
$array1 = $()
foreach($f in $FILES) {
Get-Content -Path $f |
Select-String -Pattern "Ad Id.*" |
ForEach-Object { $array1 += @($_.Matches.Value) }
}
$FILES.Count
$array1.Count
$array1
试试这个:
$files = ls "*.txt"
$dictionary = @{}
foreach($f in $files) {
$in = Get-Content $f
$in.Split([Environment]::NewLine) | ForEach-Object {
$key,$value = $_.Split(':')
$dictionary[$key] = $value
}
$dictionary['Ad Id'] + ', ' + $dictionary['Ad placement'] + ', ' + $dictionary['Ad Text'] | Out-File -FilePath '.\results.csv' -Append
}
排序输出:
$files = ls "fil*.txt"
$dictionary = @{}
[System.Collections.Generic.List[String]]$list = @()
foreach($f in $files) {
$in = Get-Content $f
$in.Split([Environment]::NewLine) | ForEach-Object {
$key,$value = $_.Split(':')
$dictionary[$key] = $value
}
[void]$list.Add( $dictionary['Ad Id'] + ', ' + $dictionary['Ad placement'] + ', ' + $dictionary['Ad Text'] )
}
[void]$list.Sort()
$list | Out-File -FilePath '.\results.csv' -Append
另一种略有不同的方法。
- RegEx 解析 $Line 并创建一个变量,其名称在冒号前(不带
Ad
),值在冒号后 - 在每个处理过的文件之后,变量都作为自定义对象输出
$Data = ForEach ($File in (Get-ChildItem File*.txt)){
$Id,$Text,$Placement="","",""
ForEach ($Line in (Get-Content $File)){
If ($Line -Match "AD (?<Label>.*?): (?<Value>.*)"){
Set-Variable -Name "$($Matches.Label)" -Value $Matches.Value
}
}
[PSCustomObject]@{ID = $Id
Placement = $placement
Text = $Text}
}
$Data
$Data | Export-CSv ".\Result.csv" -NoTypeInformation
示例输出:
ID Placement Text
-- --------- ----
xxxx spaceship blah blah
yyyy zoo blah blah