My end goal is to convert/export data in some CSV files into an Excel workbook with multiple sheets using PowerShell. I have it about 90% working but I cannot seem to remove some commas in the contents of the CSV files.
I've tried some regular expressions but they didn't work.
My luck so far is that it either removes nothing or it removes every single comma in the CSV and then breaks the export to Excel by putting everything into a single column or it removes everything in the cell that has the comma in it.
Here's an example of some of the content of the CSV that I am trying to work with.
Software Name Vendor
Software A Vendor A
Software B Vendor B
Software C Vendor, C
Software D Vendor D
Software E Vendor, E
Here is a snippet of code from a script that I am putting together.
$Excel = new-object -comobject Excel.Application
$Excel.SheetsInNewWorkbook = $GetCSV.Count
$AddWorkBook = $Excel.Workbooks.Add()
$NewWorkSheet=1
foreach ($CSV in $GetCSV) {
(Get-Content $CSV | Select-Object -Skip 1) | Set-Content $CSV
$Row=1
$Column=1
$WorkSheet = $AddWorkBook.WorkSheets.Item($NewWorkSheet)
$Name = $CSV.Name -replace ('.CSV','')
$WorkSheet.Name = $Name
$GetFile = (Get-Content $CSV)
foreach($Line in $GetFile) {
$LineContens=$Line -split ‘,(?!\s*\w+”)’
foreach($Cell in $LineContens) {
$WorkSheet.Cells.Item($Row,$Column) = $Cell
$Column++
}
$Column=1
$Row++
}
$NewWorkSheet++
}
$AddWorkBook.SaveAs($WorkBookName)
,(?!(?<=",)")
Replace With: nothing
This regular expression will do the following:
Live Demo
https://regex101.com/r/uY6iG1/2
Sample text
"Software Name","Vendor"
"Software A","Vendor A"
"Software B","Vendor B"
"Software C","Vendor, C"
"Software D,","Vendor D"
"Soft,ware E","Vendor, E"
After Replacement
"Software Name","Vendor"
"Software A","Vendor A"
"Software B","Vendor B"
"Software C","Vendor C"
"Software D","Vendor D"
"Software E","Vendor E"
NODE EXPLANATION
----------------------------------------------------------------------
, ','
----------------------------------------------------------------------
(?! look ahead to see if there is not:
----------------------------------------------------------------------
(?<= look behind to see if there is:
----------------------------------------------------------------------
", '",'
----------------------------------------------------------------------
) end of look-behind
----------------------------------------------------------------------
" '"'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.