我有一个充满这样行的文件。分号的数量总是相同的,并且在第一个分号之前总是有一个 3 个字符的字符串。
RT2;SS1234567;INV RED;13.06.2021;14.06.2021;154;Out;
RT2;XX1234567;INV RED;04.05.2021;14.06.2021;1472;Out;
RT2;FF1234567;INV RED;04.05.2021;14.06.2021;1472;Out;
RT2;LL1234567;INV RED;13.05.2021;14.06.2021;1472;Out;
我想从每行中删除开头的 3 个字符串和分号。
这就是我拉入文件的方式,它充满了我需要删除的空行和行
#import the file removing the first row and removing blank rows
$inFile = Get-Content -Path ($InFileDir + $InFileName)|Select-Object -Skip 1|? {$_.trim() -ne "" }
# Removes the (12334 rows affected) line that's added by sql
$inFile = $inFile|Where-Object {$_ -notlike '(*)'}
# Source file is two different sql table exports appended to each other, store the different headers
$header1 = 'RT1;Polref;Tranaction;Eff Dte;Process Dte;Fund;Movement;'
$header2 = 'RT3;Polref;Tranaction;Eff Dte;Process Dte;Fund;Qty;Amt;'
#Get some file positions
$RowBeforeheader2Index = $InFile.IndexOf($header2) -1
$header1Index = $InFile.IndexOf($header1)
$header2Index = $InFile.IndexOf($header2)
$LastRow = $inFile.Length -1
$outFile[$header1Index..$RowBeforeheader2Index]
foreach ($row in $outFile)
{
//perform a substring on the row and add to $var
}
$var|Out-file 'C:\temp\output.txt'
我不确定如何填写 foreach 循环以达到我想要的结果。
有很多方法可以做到这一点。如果你的操作真的像删除第一列那么简单,你可以这样做。假设$outFile
您的示例中的内容对应于您的列表并且$var = @()
已在您的脚本中设置,您可以将以下内容放入您的 foreach 循环中:
$null,$row = $row -split ';' # Turn the string into an array and dump the first element.
$var += $row -join ';' # Turn the array into a string using ; as delimiter
的内容$var
应该是这样的:
SS1234567;INV RED;13.06.2021;14.06.2021;154;Out;
XX1234567;INV RED;04.05.2021;14.06.2021;1472;Out;
FF1234567;INV RED;04.05.2021;14.06.2021;1472;Out;
LL1234567;INV RED;13.05.2021;14.06.2021;1472;Out;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句