如何将多行字符串导出为单行文本
How to export multi line string to single line text
我有一个文本文件"c:\zz.txt",摘录如下:
#N ABSA ALL ROUNDER FoF
#D UT-ABSAAG
#P 20190215 393.83 393.83 0.00
#N ABSA BALANCED FUND
#D UT-ABSABA
#P 20190215 432.28 432.28 0.00
#N COMMUNITY GILT
#D UT-COM-G
#P 20190215 151.21 151.21 8.59
我想将其导出为以下格式的 .TXT 文件:
UT_Name,UT_Code,Date,Value1,Value2,Vol
ABSA ALL ROUNDER FoF,UT-ABSAAG,20190215,393.83,393.83,0
ABSA BALANCED FUND,UT-ABSABA,20190215,432.28,432.28,0
COMMUNITY GILT,UT-COM-G,20190215,151.21,151.21,8.59
下面是我的代码
clear-Host
get-content -raw "c:\zz.txt" | % { $_ -replace '(#N)',"`r`n"} |% { $_ -replace '(#D|#P)',','}|Set-Content ZZ1.txt
输出:
ABSA ALL ROUNDER FoF, UT-ABSAAG, 20190215 393.83 393.83 0.00
ABSA BALANCED FUND, UT-ABSABA, 20190215 432.28 432.28 0.00
COMMUNITY GILT, UT-COM-G, 20190215 151.21 151.21 8.59
问题:
问题原来是原始数据集中的数据集“#P”是固定长度的,这意味着我不能简单地用逗号替换所有空格,因为这也会影响我上面的字段名称“#N”和“#D”不想影响。
如何有选择地替换空格?
这使用命名的捕获组来获取项目,然后将其导出到 CSV 文件。
# fake reading in a raw text file
# in real life, use Get-Content -Raw
$InStuff = @'
#N ABSA ALL ROUNDER FoF
#D UT-ABSAAG
#P 20190215 393.83 393.83 0.00
#N ABSA BALANCED FUND
#D UT-ABSABA
#P 20190215 432.28 432.28 0.00
#N COMMUNITY GILT
#D UT-COM-G
#P 20190215 151.21 151.21 8.59
'@
# split into blocks, trim unwanted whitespace, filter out the blank block
$SplitInStuff = ($InStuff -split '#N').Trim().Where({$_})
$Results = foreach ($SIS_Item in $SplitInStuff)
{
$Null = $SIS_Item -match '(?sm)(?<UT_Name>^.+$).*#D (?<UT_Code>.+).*#P (?<Date>\d+)\s+(?<Value1>[0-9.]+)\s+(?<Value2>[0-9.]+)\s+(?<Vol>[0-9.]+)'
[PSCustomObject]@{
# the ".Trim()" was needed to remove leftover EOL/NewLine/space chars
UT_Name = $Matches.UT_Name.Trim()
UT_Code = $Matches.UT_Code.Trim()
Date = $Matches.Date.Trim()
Value1 = $Matches.Value1.Trim()
Value2 = $Matches.Value2.Trim()
Vol = $Matches.Vol.Trim()
}
}
$Results |
Export-Csv -LiteralPath "$env:TEMP\milkywaypizza_ProductInfo.csv" -NoTypeInformation
CSV 文件的内容...
"UT_Name","UT_Code","Date","Value1","Value2","Vol"
"ABSA ALL ROUNDER FoF","UT-ABSAAG","20190215","393.83","393.83","0.00"
"ABSA BALANCED FUND","UT-ABSABA","20190215","432.28","432.28","0.00"
"COMMUNITY GILT","UT-COM-G","20190215","151.21","151.21","8.59"
其他方法:
$Data = @'
#N ABSA ALL ROUNDER FoF
#D UT-ABSAAG
#P 20190215 393.83 393.83 0.00
#N ABSA BALANCED FUND
#D UT-ABSABA
#P 20190215 432.28 432.28 0.00
#N COMMUNITY GILT
#D UT-COM-G
#P 2019021 151.21 151.21 8.59
'@
#template for learn schema
$template=@'
#N {Vol_ABSA_ALL_ROUNDER_FoF*:ABSA ALL ROUNDER FoF}
#D UT-{UT_Name:ABSAAG}
#P {Date:20190215} {Value1:393.83} {Value2:393.83} {Vol:0.00}
#N {Vol_ABSA_ALL_ROUNDER_FoF*:ABSA ALL ROUNDER FoF 2}
#D UT-{UT_Name:ABSAAG2}
#P {Date:20190216} {Value1:393.83} {Value2:393.83} {Vol:0.00}
'@
$Data | ConvertFrom-String -TemplateContent $template | export-csv "c:\temp\result.csv" -NoType
如何有选择地替换多个空白字符?
例如,仅选择带有量词的多个空格 -replace " {2,}",','
以下脚本基于您的一个班轮:
## Q:\Test19\SO_55387785.ps1
$FileIn = '.\zz.txt'
$FileOut= '.\zz1.txt'
Set-Content $FileOut -Value "UT_Name,UT_Code,Date,Value1,Value2,Vol"
(Get-Content $FileIn -raw) -replace "(`r?`n)?#N " -replace "`r?`n(#D|#P) | {2,}",',' |
Add-Content $FileOut
产生这个输出:
> Get-Content .\ZZ1.txt
UT_Name,UT_Code,Date,Value1,Value2,Vol
ABSA ALL ROUNDER FoF,UT-ABSAAG,20190215,393.83,393.83,0.00
ABSA BALANCED FUND,UT-ABSABA,20190215,432.28,432.28,0.00
COMMUNITY GILT,UT-COM-G,20190215,151.21,151.21,8.59
我有一个文本文件"c:\zz.txt",摘录如下:
#N ABSA ALL ROUNDER FoF
#D UT-ABSAAG
#P 20190215 393.83 393.83 0.00
#N ABSA BALANCED FUND
#D UT-ABSABA
#P 20190215 432.28 432.28 0.00
#N COMMUNITY GILT
#D UT-COM-G
#P 20190215 151.21 151.21 8.59
我想将其导出为以下格式的 .TXT 文件:
UT_Name,UT_Code,Date,Value1,Value2,Vol
ABSA ALL ROUNDER FoF,UT-ABSAAG,20190215,393.83,393.83,0
ABSA BALANCED FUND,UT-ABSABA,20190215,432.28,432.28,0
COMMUNITY GILT,UT-COM-G,20190215,151.21,151.21,8.59
下面是我的代码
clear-Host
get-content -raw "c:\zz.txt" | % { $_ -replace '(#N)',"`r`n"} |% { $_ -replace '(#D|#P)',','}|Set-Content ZZ1.txt
输出:
ABSA ALL ROUNDER FoF, UT-ABSAAG, 20190215 393.83 393.83 0.00
ABSA BALANCED FUND, UT-ABSABA, 20190215 432.28 432.28 0.00
COMMUNITY GILT, UT-COM-G, 20190215 151.21 151.21 8.59
问题: 问题原来是原始数据集中的数据集“#P”是固定长度的,这意味着我不能简单地用逗号替换所有空格,因为这也会影响我上面的字段名称“#N”和“#D”不想影响。
如何有选择地替换空格?
这使用命名的捕获组来获取项目,然后将其导出到 CSV 文件。
# fake reading in a raw text file
# in real life, use Get-Content -Raw
$InStuff = @'
#N ABSA ALL ROUNDER FoF
#D UT-ABSAAG
#P 20190215 393.83 393.83 0.00
#N ABSA BALANCED FUND
#D UT-ABSABA
#P 20190215 432.28 432.28 0.00
#N COMMUNITY GILT
#D UT-COM-G
#P 20190215 151.21 151.21 8.59
'@
# split into blocks, trim unwanted whitespace, filter out the blank block
$SplitInStuff = ($InStuff -split '#N').Trim().Where({$_})
$Results = foreach ($SIS_Item in $SplitInStuff)
{
$Null = $SIS_Item -match '(?sm)(?<UT_Name>^.+$).*#D (?<UT_Code>.+).*#P (?<Date>\d+)\s+(?<Value1>[0-9.]+)\s+(?<Value2>[0-9.]+)\s+(?<Vol>[0-9.]+)'
[PSCustomObject]@{
# the ".Trim()" was needed to remove leftover EOL/NewLine/space chars
UT_Name = $Matches.UT_Name.Trim()
UT_Code = $Matches.UT_Code.Trim()
Date = $Matches.Date.Trim()
Value1 = $Matches.Value1.Trim()
Value2 = $Matches.Value2.Trim()
Vol = $Matches.Vol.Trim()
}
}
$Results |
Export-Csv -LiteralPath "$env:TEMP\milkywaypizza_ProductInfo.csv" -NoTypeInformation
CSV 文件的内容...
"UT_Name","UT_Code","Date","Value1","Value2","Vol"
"ABSA ALL ROUNDER FoF","UT-ABSAAG","20190215","393.83","393.83","0.00"
"ABSA BALANCED FUND","UT-ABSABA","20190215","432.28","432.28","0.00"
"COMMUNITY GILT","UT-COM-G","20190215","151.21","151.21","8.59"
其他方法:
$Data = @'
#N ABSA ALL ROUNDER FoF
#D UT-ABSAAG
#P 20190215 393.83 393.83 0.00
#N ABSA BALANCED FUND
#D UT-ABSABA
#P 20190215 432.28 432.28 0.00
#N COMMUNITY GILT
#D UT-COM-G
#P 2019021 151.21 151.21 8.59
'@
#template for learn schema
$template=@'
#N {Vol_ABSA_ALL_ROUNDER_FoF*:ABSA ALL ROUNDER FoF}
#D UT-{UT_Name:ABSAAG}
#P {Date:20190215} {Value1:393.83} {Value2:393.83} {Vol:0.00}
#N {Vol_ABSA_ALL_ROUNDER_FoF*:ABSA ALL ROUNDER FoF 2}
#D UT-{UT_Name:ABSAAG2}
#P {Date:20190216} {Value1:393.83} {Value2:393.83} {Vol:0.00}
'@
$Data | ConvertFrom-String -TemplateContent $template | export-csv "c:\temp\result.csv" -NoType
如何有选择地替换多个空白字符?
例如,仅选择带有量词的多个空格 -replace " {2,}",','
以下脚本基于您的一个班轮:
## Q:\Test19\SO_55387785.ps1
$FileIn = '.\zz.txt'
$FileOut= '.\zz1.txt'
Set-Content $FileOut -Value "UT_Name,UT_Code,Date,Value1,Value2,Vol"
(Get-Content $FileIn -raw) -replace "(`r?`n)?#N " -replace "`r?`n(#D|#P) | {2,}",',' |
Add-Content $FileOut
产生这个输出:
> Get-Content .\ZZ1.txt
UT_Name,UT_Code,Date,Value1,Value2,Vol
ABSA ALL ROUNDER FoF,UT-ABSAAG,20190215,393.83,393.83,0.00
ABSA BALANCED FUND,UT-ABSABA,20190215,432.28,432.28,0.00
COMMUNITY GILT,UT-COM-G,20190215,151.21,151.21,8.59