簡體   English   中英

在文件的每一行中添加逗號

[英]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.

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