[英]adding comma to every line in a file
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. 您用
foreach
-loop的每次迭代覆盖文件。 Place Set-Content
outside of the loop, as well as the $collate
variable. 将
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.