I am having trouble placing "," after the end of each line using the following code. Any thoughts, please ?
$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
}
Sample data in file is as below :
www.google.com
www.yahoo.com
1533080972
1533080971
https://www.tamu.edu/
You can do this with a one-liner:
gc data.txt | %{$_ -replace '$',','} | out-file dataoutput.txt
Get the content of the file, go through line by line, replace the end of the line with a comma, and output to the new file.
You overwrite the file with every iteration of your foreach
-loop. Place Set-Content
outside of the loop, as well as the $collate
variable.
$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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.