\nI have a csv that has a category, followed by a header row, data, then a total row. The next category comes after a blank line. This pattern repeats. I would like to reformat this to another file to use in a spreadsheet.<\/p>\n<\/blockquote>\n<\/aside>\n
just so you know, that’s not a valid CSV format. Sure, the file has a ‘.csv’ ending, but the format in it, is NOT CSV. THat’s why ‘import-csv’ does not work.<\/p>\n
You have to use ‘get-content’ and iterate through it.<\/p>","upvoteCount":0,"datePublished":"2022-01-15T20:18:04.000Z","url":"https://community.spiceworks.com/t/reformat-csv-file-with-2-line-header-with-powershell/822177/3","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
Thanks for the replies. I should have been a little more descriptive in my request. I mainly wanted to know if this was possible and if anyone has ever seen this type of csv format. Usually, when I see these issues, I can have someone review them and suggest a method that I can try.<\/p>\n
I started with this, but it only sees the first line which is the category, then produces blanks with commas<\/p>\n
Import-Csv .\\2021_cat_trans.csv |\nForEach-Object {\n Write-Host \"$($_.Date), $($_.Ref), $($_.Payee), $($_.Amount), $($_.Note).\"\n}\n<\/code><\/pre>\nThis line will pull just the date in the first column<\/p>\n
Import-Csv .\\2021_cat_trans.csv\n<\/code><\/pre>\nOne of the comments suggested ‘get-content’, which I can review. Sorry for the confusion. I was looking for guidance on where to start.<\/p>","upvoteCount":0,"datePublished":"2022-01-15T22:18:44.000Z","url":"https://community.spiceworks.com/t/reformat-csv-file-with-2-line-header-with-powershell/822177/4","author":{"@type":"Person","name":"richardsmith2","url":"https://community.spiceworks.com/u/richardsmith2"}},{"@type":"Answer","text":"
\"Misc Expense\"\nDate,Ref,Payee,Amount,Note\n11/04/2021,DEP,\"Vendor1\",\"5,048.34\",\"Roof\"\n12/12/2021,DEP,\"Vendor2\",\"345.97\",\"Office Supply\"\n,,\"Misc Expense Total:\",\"5394.31\",\n\n\"Misc Income\"\nDate,Ref,Payee,Amount,Note\n11/17/2021,DEP,Vendor3,\"1,000.00\",\n12/03/2021,DEP,Vendor4,\"456.00\",\n,,\"Misc Income Total:\",\"1,456.00\",\n<\/code><\/pre>\n$inFile = get-content \"D:\\TestFolder\\input.csv\"\n$data = $inFile | foreach-object { if ($_ -notlike \"\") { $_ } }\n\n$table =\nfor ($i = 0; $i -lt ($data | measure).count; $i = $I + 5) {\n for ($c = 0; $c -lt 2; $c++) {\n if($c -eq 0){\n $Category = ( $data[$i +$c] -split \",\")[0] -replace '\"'\n $Date = ( $data[$i+2+$c] -split \",\")[0]\n $Ref = ( $data[$i+2+$c] -split \",\")[1]\n $Payee = ( $data[$i+2+$c] -split \",\")[2] -replace '\"'\n $Amount = (($data[$i+2+$c] -split \",\")[3..4] -replace '\"') -join \",\"\n $Note = ( $data[$i+2+$c] -split \",\")[5] -replace '\"'\n }\n else{\n $Category = ( $data[$i] -split \",\")[0] -replace '\"'\n $Date = ( $data[$i+2+$c] -split \",\")[0]\n $Ref = ( $data[$i+2+$c] -split \",\")[1]\n $Payee = ( $data[$i+2+$c] -split \",\")[2] -replace '\"'\n $Amount = (($data[$i+2+$c] -split \",\")[3] -replace '\"') -join \",\"\n $Note = ( $data[$i+2+$c] -split \",\")[4] -replace '\"'\n }\n\n [pscustomobject]@{\n Category = $Category \n Date = $Date \n Ref = $Ref \n Payee = $Payee \n Amount = $Amount \n Note = $Note \n }\n }\n}\n\n$table\n<\/code><\/pre>\n <\/p>","upvoteCount":2,"datePublished":"2022-01-16T05:20:18.000Z","url":"https://community.spiceworks.com/t/reformat-csv-file-with-2-line-header-with-powershell/822177/5","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
Wow, this is incredible. This is a great starting place for me. This was written based on each section having 2 rows of data. The sections will have variable lengths, but I can try to work on that. I appreciate what you have done. I did not expect anyone to go this far. I hope you get credit for answering this post.<\/p>\n
Thanks again,<\/p>\n
Richard<\/p>","upvoteCount":0,"datePublished":"2022-01-16T21:25:47.000Z","url":"https://community.spiceworks.com/t/reformat-csv-file-with-2-line-header-with-powershell/822177/6","author":{"@type":"Person","name":"richardsmith2","url":"https://community.spiceworks.com/u/richardsmith2"}}]}}
Neally
(Neally)
January 15, 2022, 8:15pm
2
Welcome.
We’re happy to help, but not a script writing service.
What have you tried? where are you stuck?
If you post code, please use the ‘Insert Code’ button. Please and thank you!
Hi, and welcome to the PowerShell forum!
Don’t apologize for being a “noob” or “newbie” or “n00b.” There’s just no need – nobody will think you’re stupid, and the forums are all about asking questions. Just ask!
Use a descriptive subject. Don't say "Need help" or "PowerShell Help", actually summarize what the problem is. It helps the rest of us keep track of which problem is which.
Don’t post massive scripts. We’re all volunteers and we don’t have time to read all that, nor will we copy, past…