Hello,<\/p>\n
I have a csv file that through manipulation has an extra space in a column that I’m looking to trim.<\/p>\n
I have tried:<\/p>\n
$Trim = Import-Csv $File | ForEach-Object{$_.PSObject.Properties | ForEach-Object{$_.EEpayment = $_.EEPayment.Trim()}}\n<\/code><\/pre>\nbut get, “You cannot call a method on a null-valued expression.”<\/p>","upvoteCount":3,"answerCount":8,"datePublished":"2017-10-31T13:01:27.000Z","author":{"@type":"Person","name":"ryanstanford2","url":"https://community.spiceworks.com/u/ryanstanford2"},"acceptedAnswer":{"@type":"Answer","text":"\n\n
<\/div>\n
Ryan5204:<\/div>\n
\n\n\n
<\/div>\n
Evan7191:<\/div>\n
\nTry something like this:<\/p>\n
$Trim = Import-Csv $File\n$Trim = $Trim -ireplace '\\s,',','\n$Trim | Out-File drive:\\path\\file.csv\n<\/code><\/pre>\nThat will find and remove any space that appears immediately before a comma. It will not affect spaces that are not followed by a comma, and it will not affect commas that do not have a space immediately before them.<\/p>\n<\/blockquote>\n<\/aside>\n
This exported the length of every row, but it did head me in the right direction. I added the ‘\\s’ replace as part of the export back to the file. I’m sorry if that doesn’t make sense, but it works in my code so… Hoozah!<\/p>\n
$Trim | ConvertTo-Csv -NoTypeInformation | ForEach-Object{$_ -replace '\\s',','} | ForEach-Object{$_ -replace '[\"()$-]', ''} | Out-File $File -fo -en ascii\n<\/code><\/pre>\n<\/blockquote>\n<\/aside>\nYou should use “\\s,” because \\s by itself will replace every space, which you may not want. \\s, will remove only spaces followed by a comma, which indicates that the space is at the end of a column.<\/p>","upvoteCount":0,"datePublished":"2017-10-31T17:59:25.000Z","url":"https://community.spiceworks.com/t/trim-of-csv-column-data-stating-null-value-expression/615510/7","author":{"@type":"Person","name":"Evan7191","url":"https://community.spiceworks.com/u/Evan7191"}},"suggestedAnswer":[{"@type":"Answer","text":"
Hello,<\/p>\n
I have a csv file that through manipulation has an extra space in a column that I’m looking to trim.<\/p>\n
I have tried:<\/p>\n
$Trim = Import-Csv $File | ForEach-Object{$_.PSObject.Properties | ForEach-Object{$_.EEpayment = $_.EEPayment.Trim()}}\n<\/code><\/pre>\nbut get, “You cannot call a method on a null-valued expression.”<\/p>","upvoteCount":3,"datePublished":"2017-10-31T13:01:27.000Z","url":"https://community.spiceworks.com/t/trim-of-csv-column-data-stating-null-value-expression/615510/1","author":{"@type":"Person","name":"ryanstanford2","url":"https://community.spiceworks.com/u/ryanstanford2"}},{"@type":"Answer","text":"
Do you even need that first foreach-object:<\/p>\n
Start smaller…<\/p>\n
Import-Csv $File | \n Select-Object *, @{n=EEpayTrim; e={$_.EEPayment.Trim()}}\n<\/code><\/pre>\nsee if this gives anything<\/p>","upvoteCount":0,"datePublished":"2017-10-31T13:12:52.000Z","url":"https://community.spiceworks.com/t/trim-of-csv-column-data-stating-null-value-expression/615510/2","author":{"@type":"Person","name":"psophos","url":"https://community.spiceworks.com/u/psophos"}},{"@type":"Answer","text":"
Is the space at the beginning, middle, or end of the column?<\/p>","upvoteCount":0,"datePublished":"2017-10-31T13:20:31.000Z","url":"https://community.spiceworks.com/t/trim-of-csv-column-data-stating-null-value-expression/615510/3","author":{"@type":"Person","name":"Evan7191","url":"https://community.spiceworks.com/u/Evan7191"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Evan7191:<\/div>\n
\nIs the space at the beginning, middle, or end of the column?<\/p>\n<\/blockquote>\n<\/aside>\n
End of the column, and it appears to only show if the csv is opened in notepad, or converted to a different format that is opened in notepad(like my script eventually does do).<\/p>","upvoteCount":0,"datePublished":"2017-10-31T14:05:35.000Z","url":"https://community.spiceworks.com/t/trim-of-csv-column-data-stating-null-value-expression/615510/4","author":{"@type":"Person","name":"ryanstanford2","url":"https://community.spiceworks.com/u/ryanstanford2"}},{"@type":"Answer","text":"
Try something like this:<\/p>\n
$Trim = Import-Csv $File\n$Trim = $Trim -ireplace '\\s,',','\n$Trim | Out-File drive:\\path\\file.csv\n<\/code><\/pre>\nThat will find and remove any space that appears immediately before a comma. It will not affect spaces that are not followed by a comma, and it will not affect commas that do not have a space immediately before them.<\/p>","upvoteCount":0,"datePublished":"2017-10-31T14:46:39.000Z","url":"https://community.spiceworks.com/t/trim-of-csv-column-data-stating-null-value-expression/615510/5","author":{"@type":"Person","name":"Evan7191","url":"https://community.spiceworks.com/u/Evan7191"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Evan7191:<\/div>\n
\nTry something like this:<\/p>\n
$Trim = Import-Csv $File\n$Trim = $Trim -ireplace '\\s,',','\n$Trim | Out-File drive:\\path\\file.csv\n<\/code><\/pre>\nThat will find and remove any space that appears immediately before a comma. It will not affect spaces that are not followed by a comma, and it will not affect commas that do not have a space immediately before them.<\/p>\n<\/blockquote>\n<\/aside>\n
This exported the length of every row, but it did head me in the right direction. I added the ‘\\s’ replace as part of the export back to the file. I’m sorry if that doesn’t make sense, but it works in my code so… Hoozah!<\/p>\n
$Trim | ConvertTo-Csv -NoTypeInformation | ForEach-Object{$_ -replace '\\s',','} | ForEach-Object{$_ -replace '[\"()$-]', ''} | Out-File $File -fo -en ascii\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2017-10-31T17:31:26.000Z","url":"https://community.spiceworks.com/t/trim-of-csv-column-data-stating-null-value-expression/615510/6","author":{"@type":"Person","name":"ryanstanford2","url":"https://community.spiceworks.com/u/ryanstanford2"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Evan7191:<\/div>\n
\n\nYou should use “\\s,” because \\s by itself will replace every space, which you may not want. \\s, will remove only spaces followed by a comma, which indicates that the space is at the end of a column.<\/p>\n<\/blockquote>\n<\/blockquote>\n<\/aside>\n
Good catch, that was a typo on my part. The specific file I was using didn’t have any other spaces, but that’s more by chance than anything.<\/p>","upvoteCount":0,"datePublished":"2017-10-31T19:01:00.000Z","url":"https://community.spiceworks.com/t/trim-of-csv-column-data-stating-null-value-expression/615510/8","author":{"@type":"Person","name":"ryanstanford2","url":"https://community.spiceworks.com/u/ryanstanford2"}}]}}
Hello,
I have a csv file that through manipulation has an extra space in a column that I’m looking to trim.
I have tried:
$Trim = Import-Csv $File | ForEach-Object{$_.PSObject.Properties | ForEach-Object{$_.EEpayment = $_.EEPayment.Trim()}}
but get, “You cannot call a method on a null-valued expression.”
3 Spice ups
psophos
(M Boyle)
October 31, 2017, 1:12pm
2
Do you even need that first foreach-object:
Start smaller…
Import-Csv $File |
Select-Object *, @{n=EEpayTrim; e={$_.EEPayment.Trim()}}
see if this gives anything
Evan7191
(Evan7191)
October 31, 2017, 1:20pm
3
Is the space at the beginning, middle, or end of the column?
End of the column, and it appears to only show if the csv is opened in notepad, or converted to a different format that is opened in notepad(like my script eventually does do).
Evan7191
(Evan7191)
October 31, 2017, 2:46pm
5
Try something like this:
$Trim = Import-Csv $File
$Trim = $Trim -ireplace '\s,',','
$Trim | Out-File drive:\path\file.csv
That will find and remove any space that appears immediately before a comma. It will not affect spaces that are not followed by a comma, and it will not affect commas that do not have a space immediately before them.
Evan7191:
Try something like this:
$Trim = Import-Csv $File
$Trim = $Trim -ireplace '\s,',','
$Trim | Out-File drive:\path\file.csv
That will find and remove any space that appears immediately before a comma. It will not affect spaces that are not followed by a comma, and it will not affect commas that do not have a space immediately before them.
This exported the length of every row, but it did head me in the right direction. I added the ‘\s’ replace as part of the export back to the file. I’m sorry if that doesn’t make sense, but it works in my code so… Hoozah!
$Trim | ConvertTo-Csv -NoTypeInformation | ForEach-Object{$_ -replace '\s',','} | ForEach-Object{$_ -replace '["()$-]', ''} | Out-File $File -fo -en ascii
Evan7191
(Evan7191)
October 31, 2017, 5:59pm
7
Ryan5204:
Evan7191:
Try something like this:
$Trim = Import-Csv $File
$Trim = $Trim -ireplace '\s,',','
$Trim | Out-File drive:\path\file.csv
That will find and remove any space that appears immediately before a comma. It will not affect spaces that are not followed by a comma, and it will not affect commas that do not have a space immediately before them.
This exported the length of every row, but it did head me in the right direction. I added the ‘\s’ replace as part of the export back to the file. I’m sorry if that doesn’t make sense, but it works in my code so… Hoozah!
$Trim | ConvertTo-Csv -NoTypeInformation | ForEach-Object{$_ -replace '\s',','} | ForEach-Object{$_ -replace '["()$-]', ''} | Out-File $File -fo -en ascii
You should use “\s,” because \s by itself will replace every space, which you may not want. \s, will remove only spaces followed by a comma, which indicates that the space is at the end of a column.
Evan7191:
You should use “\s,” because \s by itself will replace every space, which you may not want. \s, will remove only spaces followed by a comma, which indicates that the space is at the end of a column.
Good catch, that was a typo on my part. The specific file I was using didn’t have any other spaces, but that’s more by chance than anything.