[英]adding comma to every line in a file
我在使用以下代碼在每行末尾放置“,”時遇到麻煩。 有什么想法嗎?
$inputFile = Get-Content "C:\PowerShell Automation\data.txt"
$outputFile = "C:\PowerShell Automation\dataoutput.txt"
foreach($Obj in $inputFile)
{
$begin = ""
$end = ","
$collate = $begin + $Obj + $end
Set-Content -path $outputFile -value $collate
}
文件中的示例數據如下:
www.google.com
www.yahoo.com
1533080972
1533080971
https://www.tamu.edu/
您可以單線執行此操作:
gc data.txt | %{$_ -replace '$',','} | out-file dataoutput.txt
獲取文件的內容,逐行瀏覽,用逗號替換行尾,然后輸出到新文件。
您用foreach
-loop的每次迭代覆蓋文件。 將Set-Content
以及$collate
變量放置在循環之外。
$inputFile = Get-Content "C:\PowerShell Automation\data.txt"
$outputFile = "C:\PowerShell Automation\dataoutput.txt"
$collate = foreach($Obj in $inputFile) {
$begin = ""
$end = ","
$begin + $Obj + $end
}
Set-Content -path $outputFile -value $collate
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.