我目前正在尝试使用 Powershell 获取一些原始数据并将其按摩到 SQL 插入语句中。第一个任务是删除每行原始数据末尾的两个逗号(在数据之前和之后拍 SQL 代码之前)。我一直在使用 Powershell 'TrimEnd' 来尝试完成这项工作,但以下迭代似乎并没有从行尾删除第一个逗号:
Sample 1 - (get-content -ErrorAction "Stop" source-file.txt) | Foreach-Object -ErrorAction "Stop" {$_.TrimEnd()} | out-file destination-file.sql
Sample 2 - (get-content -ErrorAction "Stop" source-file.txt) | Foreach-Object -ErrorAction "Stop" {$_.TrimEnd(',')} | out-file destination-file.sql
Sample 3 - (get-content -ErrorAction "Stop" source-file.txt) | Foreach-Object -ErrorAction "Stop" {$_.TrimEnd(",")} | out-file destination-file.sql
Sample 4 - (get-content -ErrorAction "Stop" source-file.txt) | Foreach -ErrorAction "Stop" {$_.TrimEnd()} | out-file destination-file.sql
source-file.txt 看起来像这样:
富,富1,富2,富3,富4,富5,,
我希望最终得到的是:
富,富1,富2,富3,富4,富5,
时代200K
问题是我没有逃避角色还是“Foreach/Foreach-Object”不是这里的方法?在删除 source-file.txt 文件末尾的两个逗号之前,我想确保我走的是正确的道路。