Hi,<\/p>\n
I have a CSV file with 25 columns. I need to add [ and ] to the beginning and end respectively to each value in column 2 and in column 9. The values of column 2 will always be 12 digits and in column 9, values can either be 19 digits long or 18 digits long.<\/p>\n
I tried the following to start:<\/p>\n
import-csv $cardExpiryFile | Select-Object *,@{Expression={\"[$($_.column2)]\"}} | export-csv -notypeinformation $newCardExpiryFile -Append\n<\/code><\/pre>\nBut it only changed the value in column 2, row 2.<\/p>\n
I then tried:<\/p>\n
import-csv $cardExpiryFile | ForEach-Object {\n\n Select-Object *,@{Expression={\"[$($_.column2)]\"}} | export-csv -notypeinformation $newCardExpiryFile -Append\n \n}\n<\/code><\/pre>\nBut the resulting csv file was blank.<\/p>\n
I then tried the following just to see what was being returned without exporting it to a CSV. I stepped through the code and see that the $changeddata variable is always blank.<\/p>\n
import-csv $cardExpiryFile | ForEach-Object {\n\n $changeddata = Select-Object *,@{Expression={\"[$($_.column2)]\"}} \n \n}\n<\/code><\/pre>\nAny ideas?<\/p>","upvoteCount":5,"answerCount":6,"datePublished":"2021-08-26T20:24:55.000Z","author":{"@type":"Person","name":"spiceuser-wq94h","url":"https://community.spiceworks.com/u/spiceuser-wq94h"},"suggestedAnswer":[{"@type":"Answer","text":"
Hi,<\/p>\n
I have a CSV file with 25 columns. I need to add [ and ] to the beginning and end respectively to each value in column 2 and in column 9. The values of column 2 will always be 12 digits and in column 9, values can either be 19 digits long or 18 digits long.<\/p>\n
I tried the following to start:<\/p>\n
import-csv $cardExpiryFile | Select-Object *,@{Expression={\"[$($_.column2)]\"}} | export-csv -notypeinformation $newCardExpiryFile -Append\n<\/code><\/pre>\nBut it only changed the value in column 2, row 2.<\/p>\n
I then tried:<\/p>\n
import-csv $cardExpiryFile | ForEach-Object {\n\n Select-Object *,@{Expression={\"[$($_.column2)]\"}} | export-csv -notypeinformation $newCardExpiryFile -Append\n \n}\n<\/code><\/pre>\nBut the resulting csv file was blank.<\/p>\n
I then tried the following just to see what was being returned without exporting it to a CSV. I stepped through the code and see that the $changeddata variable is always blank.<\/p>\n
import-csv $cardExpiryFile | ForEach-Object {\n\n $changeddata = Select-Object *,@{Expression={\"[$($_.column2)]\"}} \n \n}\n<\/code><\/pre>\nAny ideas?<\/p>","upvoteCount":5,"datePublished":"2021-08-26T20:24:55.000Z","url":"https://community.spiceworks.com/t/manipulate-data-in-certain-columns-csv-file-powershell/809549/1","author":{"@type":"Person","name":"spiceuser-wq94h","url":"https://community.spiceworks.com/u/spiceuser-wq94h"}},{"@type":"Answer","text":"
Import-Csv -Path \"$PSScriptRoot\\2330004.csv\" | ForEach-Object {\n $_.column2 = \"[$($_.column2)]\" #set the new value of column2 from \"value\" to \"[value]\"\n $_.column9 = \"[$($_.column9)]\" #set the new value of column9 from \"value\" to \"[value]\"\n $_ # Export-CSV requires an object when piped so we 'output' our object.\n} | Export-Csv -Path \"$PSScriptRoot\\Modified.csv\" -NoTypeInformation\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2021-08-26T21:27:57.000Z","url":"https://community.spiceworks.com/t/manipulate-data-in-certain-columns-csv-file-powershell/809549/2","author":{"@type":"Person","name":"mike9019","url":"https://community.spiceworks.com/u/mike9019"}},{"@type":"Answer","text":"That is perfect! That did the trick! Thank you so much!<\/p>\n
@mike9019<\/a><\/p>","upvoteCount":0,"datePublished":"2021-08-27T17:47:25.000Z","url":"https://community.spiceworks.com/t/manipulate-data-in-certain-columns-csv-file-powershell/809549/3","author":{"@type":"Person","name":"spiceuser-wq94h","url":"https://community.spiceworks.com/u/spiceuser-wq94h"}},{"@type":"Answer","text":"Hi,<\/p>\n
I actually have another question…not sure if a new thread is required or not…I now need to exclude two columns from the CSV in addition to adding the square brackets. The two columns that need to be removed is column2 and column4. I’ve removed columns for a previous solution, but am not sure how to marry it with code to add the square brackets.<\/p>\n
I’m passing in the columns in a comma separated string as a parameter and then splitting them into an array. The parameter looks like “column2,column4” and the paramter variable is $excludeColumns.<\/p>\n
$excludeColumnsArray = $excludeColumns -split \",\"\n<\/code><\/pre>\n\n<\/code><\/pre>\nIn my other solution I simply imported the CSV, excluding the columns, and then exported the data back to a CSV.<\/p>\n
$NewCSV = Import-Csv $AuraFileFullPath | select-object * -ExcludeProperty $ExcludeColumnsArray -Verbose\n$NewCSV | Export-Csv -NoTypeInformation -LiteralPath $AuraFileFullPath -Verbose\n<\/code><\/pre>\nFor this solution, I tried the following:<\/p>\n
Import-Csv -Path $cardExpiryFile | select-object * -ExcludeProperty $excludeColumnsArray | ForEach-Object {\n\n $_.column9 = \"[$($_.column9)]\" #set the new value of column9 from \"value\" to \"[value]\"\n $_ # Export-CSV requires an object when piped so we 'output' our object. \n } | Export-Csv -Path $newCardExpiryFile -NoTypeInformation \n<\/code><\/pre>\nWhen I step through the code and view the values in $_, I see the two columns are removed and column9 is enclosed in square brackets, but the resulting CSV file is empty.<\/p>\n
I also tried<\/p>\n
Import-Csv -Path $cardExpiryFile | ForEach-Object {\n\n $_.column9 = \"[$($_.column9)]\" #set the new value of column9 from \"value\" to \"[value]\"\n $_ # Export-CSV requires an object when piped so we 'output' our object. \n } | select-object * -ExcludeProperty $excludeColumnsArray | Export-Csv -Path $newCardExpiryFile -NoTypeInformation \n<\/code><\/pre>\nWith the same result. I could import the CSV and exclude the columns and then save the file and then re-import it and add the square brackets to column9, but I’m sure there is a much more elegant way to do this.<\/p>\n