Hello - I wonder if you could be of assistance?<\/p>\n
I currently have a csv file with approx 90 + columns with headers but it also has addtional columns without any headers.<\/p>\n
So that when I import it they do not come through<\/p>\n
Is there a way I can add 15 to 20 columns headers to the end , to ensure the un named columns are included.<\/p>\n
Many thanks<\/p>\n
& regards<\/p>\n
Jo<\/p>","upvoteCount":6,"answerCount":19,"datePublished":"2016-01-18T10:03:27.000Z","author":{"@type":"Person","name":"jomoran","url":"https://community.spiceworks.com/u/jomoran"},"acceptedAnswer":{"@type":"Answer","text":"
OK, so I took the first worksheet in the spreadsheet and saved it as a CSV. I then ran this code and got what looked like a good result to me:<\/p>\n
Get-Content .\\uip_fce.csv | Select -Skip 1 | ConvertFrom-Csv -Header PROCODET,PROVSPNO,EPIORDER,STARTAGE,Sex,CLASSPAT,ADMISORC,ADMIMETH,DISDEST,DISMETH,EPIDUR,MAINSPEF,NEOCARE,TRETSPEF,DIAG_01,DIAG_02,DIAG_03,DIAG_04,DIAG_05,DIAG_06,DIAG_07,DIAG_08,DIAG_09,DIAG_10,DIAG_11,DIAG_12,DIAG_13,DIAG_14,DIAG_15,DIAG_16,DIAG_17,DIAG_18,DIAG_19,DIAG_20,DIAG_21,DIAG_22,DIAG_23,DIAG_24,OPER_01,OPER_02,OPER_03,OPER_04,OPER_05,OPER_06,OPER_07,OPER_08,OPER_09,OPER_10,OPER_11,OPER_12,OPER_13,OPER_14,OPER_15,OPER_16,OPER_17,OPER_18,OPER_19,OPER_20,OPER_21,OPER_22,OPER_23,OPER_24,CRITICALCAREDAYS,REHABILITATIONDAYS,SPCDAYS,Local_Patient_ID,CDS_Unique_Identifier,START_DATE_SPELL,END_DATE_SPELL,START_DATE_EPISODE,END_DATE_EPISODE,Consultant_Code,RowNo,FCE_HRG,GroupingMethodFlag,DominantProcedure,FCE_PBC,CalcEpidur,ReportingEPIDUR,FCETrimpoint,FCEExcessBeddays,SpellReportFlag,SpellHRG,SpellGroupingMethodFlag,SpellDominantProcedure,SpellPDiag,SpellSDiag,SpellEpisodeCount,SpellLOS,ReportingSpellLOS,SpellTrimpoint,SpellExcessBeddays,SpellCCDays,SpellPBC,UnbundledHRGs,UB02,UB03,UB04,UB05,UB06,UB07,UB08,UB09,UB10,UB11,UB12,UB13,UB14,ub15 | Export-Csv .\\uip_fce-converted.csv -NoTypeInformation\n<\/code><\/pre>\nSorry it looks a little messy, there are tricks we could use the make the code look a little cleaner but ultimately I think this will do what you want.<\/p>","upvoteCount":1,"datePublished":"2016-01-20T12:15:41.000Z","url":"https://community.spiceworks.com/t/powershell-adding-column-headers-to-the-end-of-a-csv-file/465467/11","author":{"@type":"Person","name":"martin9700","url":"https://community.spiceworks.com/u/martin9700"}},"suggestedAnswer":[{"@type":"Answer","text":"
Hello - I wonder if you could be of assistance?<\/p>\n
I currently have a csv file with approx 90 + columns with headers but it also has addtional columns without any headers.<\/p>\n
So that when I import it they do not come through<\/p>\n
Is there a way I can add 15 to 20 columns headers to the end , to ensure the un named columns are included.<\/p>\n
Many thanks<\/p>\n
& regards<\/p>\n
Jo<\/p>","upvoteCount":6,"datePublished":"2016-01-18T10:03:27.000Z","url":"https://community.spiceworks.com/t/powershell-adding-column-headers-to-the-end-of-a-csv-file/465467/1","author":{"@type":"Person","name":"jomoran","url":"https://community.spiceworks.com/u/jomoran"}},{"@type":"Answer","text":"
Can’t you just edit the file and put them in? Or is this something you do on a regular basis? How much effort have you put in on getting the source file fixed? This would seem to be the best route to go. If that’s not possible you could do something like this:<\/p>\n
$File = Get-Content c:\\path\\Original.csv\n$FirstLine = $true\n\n$Result = ForEach ($Line in $File)\n{\n If ($FirstLine)\n {\n Write-Output \"$Line,put,the,other,columns,here\"\n $FirstLine = $false\n }\n Else\n {\n Write-Output $Line\n }\n}\n\n$Result #this now has your corrected CSV, you can Export it to a CSV and read it with your script or:\n$Objects = $Result | ConvertFrom-Csv #just convert it to objects (like Import-CSV does) and begin work\n<\/code><\/pre>","upvoteCount":2,"datePublished":"2016-01-18T10:59:08.000Z","url":"https://community.spiceworks.com/t/powershell-adding-column-headers-to-the-end-of-a-csv-file/465467/2","author":{"@type":"Person","name":"martin9700","url":"https://community.spiceworks.com/u/martin9700"}},{"@type":"Answer","text":"Another option is:<\/p>\n
$Objects = Get-Content c:\\path\\originalcsv | Select -Skip 1 | ConvertFrom-CSV -Header Specify,Your,Own,Headers,Here,Must,Do,All,Of,Them\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2016-01-18T12:02:08.000Z","url":"https://community.spiceworks.com/t/powershell-adding-column-headers-to-the-end-of-a-csv-file/465467/3","author":{"@type":"Person","name":"martin9700","url":"https://community.spiceworks.com/u/martin9700"}},{"@type":"Answer","text":"Is it always the same number of columns, and if so how many columns are there?<\/p>\n
Are all the labeled columns contiguous, or are there some intervening columns that don’t have names?<\/p>","upvoteCount":2,"datePublished":"2016-01-18T17:21:57.000Z","url":"https://community.spiceworks.com/t/powershell-adding-column-headers-to-the-end-of-a-csv-file/465467/4","author":{"@type":"Person","name":"bobmccoy","url":"https://community.spiceworks.com/u/bobmccoy"}},{"@type":"Answer","text":"
Hello again<\/p>\n
in answer to your question bobMccoy there will be a maximum on this occasion of 15 columns which will range from UB_01 to UB15, ( UB standing for unbundled) & they are continguous.<\/p>\n
I have no control over the number of columns as it is an extract generated from a Governement portal.<\/p>\n
I attemtpted to no avail to get a successfull result using the code Martin so kindly provided but it was a little slow and would not import correctly<\/p>\n
I really only need to either<\/p>\n
a: remove and replace the header with a correct header<\/p>\n
b: edit the first line.<\/p>\n
c: stick with creating the table in SSIS each time<\/p>\n
the reason I am trying to do this is that it would seem there are going to be more files like this included in the upload and It would really be a boon if this process could be a little more fast & flexible.<\/p>\n
regards<\/p>\n
Jo<\/p>","upvoteCount":0,"datePublished":"2016-01-19T06:51:58.000Z","url":"https://community.spiceworks.com/t/powershell-adding-column-headers-to-the-end-of-a-csv-file/465467/5","author":{"@type":"Person","name":"jomoran","url":"https://community.spiceworks.com/u/jomoran"}},{"@type":"Answer","text":"
If you just want to jam 15 additional headers on then you can do something like one of these:<\/p>\n
$file = 'T:\\17\\in.csv'\n\n$sr = [System.IO.StreamReader]($file)\n\n$firstLine = $sr.ReadLine() + ',UB1,UB2,UB3'\n$rest = $sr.ReadToEnd()\n\n$sr.Close()\n\n$sw = [System.IO.StreamWriter]($file)\n\n$sw.WriteLine($firstLine)\n$sw.Write($rest)\n\n$sw.close()\n<\/code><\/pre>\n$file = 'T:\\17\\in.csv'\n\n$sr = [System.IO.StreamReader]($file)\n\n$firstLine = $sr.ReadLine() + ',UB1,UB2,UB3'\n\n$csv = $firstline + \"`r`n\" + $sr.ReadToEnd() |\nConvertFrom-Csv\n\n$sr.Close()\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2016-01-19T12:52:37.000Z","url":"https://community.spiceworks.com/t/powershell-adding-column-headers-to-the-end-of-a-csv-file/465467/6","author":{"@type":"Person","name":"craigduff","url":"https://community.spiceworks.com/u/craigduff"}},{"@type":"Answer","text":"Thank you soo much Cduff.<\/p>\n
Your first example appears to have worked in that it has everything in the right place.<\/p>\n
As I had this before I was dismayed when SSIS actually imported the first 10 cols of the file into the first 10 newly created UB cols and then put all the other cols as one string into one column at the end and then failed the package on a truncation error.<\/p>\n
So I am now getting over the signing issues to execute the script and then do the import.<\/p>\n
I think perhaps I will just do a quick dummy run to see if it takes the file as is.<\/p>\n
Thank you once again<\/p>\n
Jo<\/p>","upvoteCount":1,"datePublished":"2016-01-19T13:40:13.000Z","url":"https://community.spiceworks.com/t/powershell-adding-column-headers-to-the-end-of-a-csv-file/465467/7","author":{"@type":"Person","name":"jomoran","url":"https://community.spiceworks.com/u/jomoran"}},{"@type":"Answer","text":"
Morning Guys<\/p>\n
Cduff many thanks;<\/p>\n
\n- your second script didn’t seem to make any alteration at all - sorry<\/li>\n<\/ul>\n
Your first script:-<\/p>\n
So close, an unusual result : in the original last column (prior to the insertion) where there is no data it is populated with data from the first column & where there is data it concatenates to the exisiting data - novel ?<\/p>\n
the following columns are then populated with the data from the 2nd , 3rd, 4th cols etc respectively until it runs out of cols it then puts the remainder of the columns data in the last column.<\/p>\n
I am still no nearer to a final solution.<\/p>\n
Knowing that this is the way forward for this year on year process which has updates I am going to persevere.<\/p>\n
This will go in as I want it.<\/p>\n
Thanking you for your help it really is much appreciated<\/p>\n
regards<\/p>\n
Jo%uFEFF<\/p>","upvoteCount":0,"datePublished":"2016-01-20T09:20:42.000Z","url":"https://community.spiceworks.com/t/powershell-adding-column-headers-to-the-end-of-a-csv-file/465467/8","author":{"@type":"Person","name":"jomoran","url":"https://community.spiceworks.com/u/jomoran"}},{"@type":"Answer","text":"