我有一個如下所示的文本檔案,其中包含第 8 行的表格。現在表格的長度可以變化,但每行始終包含 4 個值。
輸入檔案:
0001117945
14102022
0001056.98
GBP
0000000.00
0000000.00
\\GLORSAWA01\EHIShared\Remittance\UK01\UKI_REM_COL58652cbc13ca49aabf000.pdf
<LineItemTable><LINEITEM><LINEITEMFIELD>20220916</LINEITEMFIELD><LINEITEMFIELD>2525636 </LINEITEMFIELD><LINEITEMFIELD>0.00 </LINEITEMFIELD><LINEITEMFIELD>246.05 </LINEITEMFIELD></LINEITEM><LINEITEM><LINEITEMFIELD>20220920</LINEITEMFIELD><LINEITEMFIELD>2527541 </LINEITEMFIELD><LINEITEMFIELD>0.00 </LINEITEMFIELD><LINEITEMFIELD>450.12 </LINEITEMFIELD></LINEITEM><LINEITEM><LINEITEMFIELD>20220922</LINEITEMFIELD><LINEITEMFIELD>2531147 </LINEITEMFIELD><LINEITEMFIELD>0.00 </LINEITEMFIELD><LINEITEMFIELD>360.81 </LINEITEMFIELD></LINEITEM></LineItemTable>
第 8 行中的表格結構始終相同,如下所示(格式化只是為了更好的可見性)。
<LineItemTable>
<LINEITEM>
<LINEITEMFIELD>20220916</LINEITEMFIELD>
<LINEITEMFIELD>2525636 </LINEITEMFIELD>
<LINEITEMFIELD>0.00 </LINEITEMFIELD>
<LINEITEMFIELD>246.05 </LINEITEMFIELD>
</LINEITEM>
<LINEITEM>
<LINEITEMFIELD>20220920</LINEITEMFIELD>
<LINEITEMFIELD>2527541 </LINEITEMFIELD>
<LINEITEMFIELD>0.00 </LINEITEMFIELD>
<LINEITEMFIELD>450.12 </LINEITEMFIELD>
</LINEITEM>
<LINEITEM>
<LINEITEMFIELD>20220922</LINEITEMFIELD>
<LINEITEMFIELD>2531147 </LINEITEMFIELD>
<LINEITEMFIELD>0.00 </LINEITEMFIELD>
<LINEITEMFIELD>360.81 </LINEITEMFIELD>
</LINEITEM>
</LineItemTable>
我正在嘗試從文本檔案中提取所有值并將它們寫入另一個檔案,但我想保持前 7 行不變,然后將第 8 行中的值顯示在由分隔的多行中;沒有任何空格。
所需的輸出檔案:
0001117945
14102022
0001056.98
GBP
0000000.00
0000000.00
\\GLORSAWA01\EHIShared\Remittance\UK01\UKI_REM_COL58652cbc13ca49aabf000.pdf
20220916;2525636;0.00;246.05
20220920;2527541;0.00;450.12
20220922;2531147;0.00;360.81
這是我走了多遠,但我無法將第 8 行的表格轉換為我想要的輸出。任何幫助將不勝感激。
$importfolder = ".\PowerShell_script\"
$outputfolder = ".\PowerShell_script\Output\"
$files = ".\PowerShell script\*.txt"
$list = Get-ChildItem -Path $files | select Name
$find1 = "<LineItemTable><LINEITEM><LINEITEMFIELD>"
$find2 = "</LINEITEMFIELD><LINEITEMFIELD>"
$find3 = "</LINEITEMFIELD></LINEITEM><LINEITEM><LINEITEMFIELD>"
$replace1 = ""
$replace2 = "`t"
$replace3 = "`n"
ForEach($file in $list){
echo $file.Name
$filename = $file.Name
$file = $importfolder $file.Name
$outputfile = $outputfolder $filename
$filecontent = Get-Content $file |
ForEach-Object {
if($_ -Match $find1)
{$_ -replace $replace1}
if($_ -Match $find2)
{$_ -replace $replace2}
if($_ -Match $find3)
{$_ -replace $replace3}
else {$_} # output the line as is
} | Set-Content $outputfile
}
uj5u.com熱心網友回復:
看起來這個邏輯適用于有問題的示例檔案:
$file = Get-Content path\to\examplefile.txt
$(
$file[0..($file.Length - 2)]
($file[-1] -as [xml]).SelectNodes('*/LINEITEM').ForEach{
$_.LINEITEMFIELD.Trim() -join ';'
}
)
基本上,將所有行保留到最后一行,最后一行將其視為 XML。
在你的回圈中實作這個,代碼看起來像這樣:
foreach($file in $list) {
$filename = $file.Name
$file = $importfolder $file.Name
$outputfile = $outputfolder $filename
$content = Get-Content $file
$(
$content[0..($content.Length - 2)]
($content[-1] -as [xml]).SelectNodes('*/LINEITEM').ForEach{
$_.LINEITEMFIELD.Trim() -join ';'
}
) | Set-Content $outputfile
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/532924.html
標籤:电源外壳
上一篇:如何檢查物件屬性的資料型別?
