如何将多行字符串添加到 powershell 中的数组?
How do I add multi-line strings to an array in powershell?
我正在尝试遍历一系列 txt 文件,将信息提取到基于 : 定界符的数组中。
我从文本文件中获取的所有值都放在一行中,除了一个。 ("ad text" 值)。这将切断最终输出中第 1 行之后的信息。当我预先删除换行符和换行符时, none 字段输入正确。
如何指定我的数组接受 "ad text" 字段的多行输入?
下面是我正在使用的代码:
$files = ls "*.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 Text'] + ',' + $dictionary['Ad Landing Page'] + ',' + $dictionary['Ad Targeting Location'] + ',' + $dictionary['Age'] + ',' + $dictionary['Language'] + ',' + $dictionary['Placements'] + ',' + $dictionary['Interests'] + ',' + $dictionary['Behaviors'] + ',' + $dictionary['Ad Impressions'] + ',' + $dictionary['Ad Clicks'] + ',' + $dictionary['Ad Spend'] + ',' + $dictionary['Ad Creation Date'] + ','+ $dictionary['Friends'] + ',' + $dictionary['Ad End Date'] + ',' + $dictionary['Excluded Connections'] + ',' + $dictionary['Image'] + ',' + $dictionary['Ad Targeting Location']+','+$dictionary[‘File Name’] )
}
$list | Out-File -FilePath '.\trial.csv' -Append
假设 Ad Text:
前缀行之后的附加行本身不包含 :
个字符:
# Create sample text file t.txt
@'
Ad ID:10
Ad Text:one
two
Ad Landing Page:http://example.org
'@ > t.txt
# Split the lines into field names and values by ":" and
# build a dictionary.
$dict = [ordered] @{}
$i = 0
(Get-Content -Raw t.txt) -split '(?m)^([^\n:]+):' -ne '' | ForEach-Object {
if ($i++ % 2 -eq 0) {
$key = $_
} else {
$dict[$key] = $_
}
}
# Output the dictionary, showing each entry as a list.
$dict | Format-List
输出如下,显示Ad Text
条目包含两行:
Name : Ad ID
Value : 10
Name : Ad Landing Page
Value : http://example.org
Name : Ad Text
Value : one
two
我正在尝试遍历一系列 txt 文件,将信息提取到基于 : 定界符的数组中。
我从文本文件中获取的所有值都放在一行中,除了一个。 ("ad text" 值)。这将切断最终输出中第 1 行之后的信息。当我预先删除换行符和换行符时, none 字段输入正确。
如何指定我的数组接受 "ad text" 字段的多行输入?
下面是我正在使用的代码:
$files = ls "*.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 Text'] + ',' + $dictionary['Ad Landing Page'] + ',' + $dictionary['Ad Targeting Location'] + ',' + $dictionary['Age'] + ',' + $dictionary['Language'] + ',' + $dictionary['Placements'] + ',' + $dictionary['Interests'] + ',' + $dictionary['Behaviors'] + ',' + $dictionary['Ad Impressions'] + ',' + $dictionary['Ad Clicks'] + ',' + $dictionary['Ad Spend'] + ',' + $dictionary['Ad Creation Date'] + ','+ $dictionary['Friends'] + ',' + $dictionary['Ad End Date'] + ',' + $dictionary['Excluded Connections'] + ',' + $dictionary['Image'] + ',' + $dictionary['Ad Targeting Location']+','+$dictionary[‘File Name’] )
}
$list | Out-File -FilePath '.\trial.csv' -Append
假设 Ad Text:
前缀行之后的附加行本身不包含 :
个字符:
# Create sample text file t.txt
@'
Ad ID:10
Ad Text:one
two
Ad Landing Page:http://example.org
'@ > t.txt
# Split the lines into field names and values by ":" and
# build a dictionary.
$dict = [ordered] @{}
$i = 0
(Get-Content -Raw t.txt) -split '(?m)^([^\n:]+):' -ne '' | ForEach-Object {
if ($i++ % 2 -eq 0) {
$key = $_
} else {
$dict[$key] = $_
}
}
# Output the dictionary, showing each entry as a list.
$dict | Format-List
输出如下,显示Ad Text
条目包含两行:
Name : Ad ID
Value : 10
Name : Ad Landing Page
Value : http://example.org
Name : Ad Text
Value : one
two