0

我目前正在尝试使用 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 文件末尾的两个逗号之前,我想确保我走的是正确的道路。

4

1 回答 1

0

我一定是手指发胖了,或者盯着屏幕太久了,但我从@Musaab-Al-Okaidi 的建议中利用了#2。我充分利用{$_.TrimEnd(', ,')}了我正在做的事情所需要的东西。干杯!

于 2013-02-13T22:10:03.380 回答