簡體   English   中英

從文本文件的每一行中刪除前 4 個字符

[英]Remove the first 4 characters from each row of a text file

我有一個充滿這樣行的文件。 總是有相同數量的分號,並且在第一個分號之前總是有一個 3 個字符的字符串。

RT2;SS1234567;INV 紅色;13.06.2021;14.06.2021;154;出;
RT2;XX1234567;INV 紅色;04.05.2021;14.06.2021;1472;出;
RT2;FF1234567;INV 紅色;04.05.2021;14.06.2021;1472;輸出;
RT2;LL1234567;INV 紅色;13.05.2021;14.06.2021;1472;輸出;

我想從每行中刪除開頭的 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 循環以獲得我想要的結果。 (對於這個例子,我只是稱它為 $var ......我並不是那么缺乏想象力)

編輯:

我最終將 $var 更改為列表並在 foreach 循環中使用了以下代碼

$var = New-Object System.Collections.Generic.List[System.Object]

foreach($row in $outFile)
{
    $var.Add($row.Substring(4))
}

試試這個 -

$data = @"
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;
"@ | ConvertFrom-Csv -Delimiter ";" -Header @("col1","col2", "col3", "col4", "col5", "col6", "col7")

$data | Select-Object * -ExcludeProperty col1 | ConvertTo-Csv | Select-Object -Skip 2 | Set-Content $env:USERPROFILE\Desktop\output.csv

注意 - 使用Select-Object -Skip 2 ,如果ConvertTo-Csv生成額外的列#TYPE Selected.System.Management.Automation.PSCustomObject ,否則您可以使用Select-Object -Skip 1

有很多方法可以做到這一點。 如果你的操作真的像刪除第一列那么簡單,你可以這樣做。 假設您的示例中$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;

鑒於您可以刪除固定數量的字符並假設每行至少有 4 個字符,您只需在字符串(行)數組上調用.Substring()即可:

# Sample input
$outFile = 'RT2;SS1234567', 'RT2;SS1234568', 'RT2;SS1234569'

# Remove the first 4 characters from each line (array element).
# (Use $var = ... to assign the output to a variable).
$outFile.Substring(4)

請注意,即使$outFile是一個數組.Substring()每個元素上調用.Substring()方法,這是一個稱為成員枚舉的 PowerShell 功能。

- 代替

是去除多行文本每行字符數的最簡單方法。

$content = $inFile = Get-Content -Path ($InFileDir + $InFileName)|Select-Object -Skip 1    
$content -replace "(?m)^.{4}"

多行模式由m標志啟用,因此 ^ 和 $ 將多次匹配字符串的開頭和結尾。 (除以 \n)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM