0
votes

I'm trying to take some raw data at the moment with Powershell and massage it into SQL Insert Statements. The first order of business is to remove two commas at the end of each of the lines of raw data (before slapping the SQL code before and after the data). I've been playing around with the Powershell 'TrimEnd' to try to get this done but the following iterations just don't seem to be removing even the first comma from the end of the lines:

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

The source-file.txt look something like this:

foo, foo1, foo2, foo3, foo4, foo5, ,

What I'm hoping to end up with is:

foo, foo1, foo2, foo3, foo4, foo5,

Times 200K

Is the problem that I'm not escaping the character or is the 'Foreach/Foreach-Object' not the way to go here? I'd like to make sure I'm going down the right path before I remove both commas at the end of the source-file.txt file.

1
What version of powershell are you using? Sample 2 and 3 should do the jobMusaab Al-Okaidi
Powershell Version 3.0Techie Joe
I've just tried it on Powershell 3.0 and that also works as expected. Again, it's example 2 and 3 that produce the required. I can send you a screenshot if you wantMusaab Al-Okaidi
See my answer I just posted.Techie Joe

1 Answers

0
votes

I must have fatfingered something or have been staring at the screen too long but I leveraged #2 from the suggestion from @Musaab-Al-Okaidi. I leveraged {$_.TrimEnd(', ,')} to get what I needed for what I'm doing. Cheers!