user2125062
user2125062

Reputation: 3

Remove last character (regardless of what it is) from one column in a csv file

I have a csv file with the below... I need to keep the UPC column at 13 characters (removing the last character) How would I do this in powershell. tried trimend, trim, trimstart, nothing works since it pulls out all numbers that I put in the trim statement, not just the last one.

ZONE     UPC                TPR_SALES

10       12345678901234     10.00

12       43210987655421     20.00

10       23432654654646     30.00

10       67356753677777     1.00

13       86753098675309     0.50

Upvotes: 0

Views: 1549

Answers (3)

Joseph Alcorn
Joseph Alcorn

Reputation: 2442

@mjolinor has a good suggestion, but Just be careful, if the column is not at least 13 characters, the substring message will through an error. One powershell exclusive method I use is -join $string[0..12]. It is probably more computationally intensive. As it has to expand the string to an array, enumerate an array on integers, then Jon the array to a string, but no exceptin is thrown when the string is shorter than your desired length

Upvotes: 0

Frode F.
Frode F.

Reputation: 54881

If you're importing and export the file, you could also try something like this to remove the last character. However, substring() is better if you know the exact length you want.

$_.UPC = $_.UPC -replace '(\w)$'

Upvotes: 0

mjolinor
mjolinor

Reputation: 68263

Have a look at the substring() method:

('12345678901234').substring(0,13)
1234567890123

in this example the first argument is starting position, and the second is number of characters to include.

Upvotes: 4

Related Questions