We have a SQL-server which exports a number of files on a regular basis.
For some reason the files exported via SQL agent is not a CSV file but a plain text file with som wonky formatting. And according to the people responsible for it there’s no getting around that.
I’ve been tasked with cleaning up the files and exporting them as CSV files, which I really didn’t think would be a problem. But I think I may be overthinking this because I’m not getting it right.
An example file looks like this:
Job 'Export to [SYSTEM]' : Step 1, 'SQL-query DB version (Test)' : Began Executing 2022-10-19 11:45:01
DGGRP DGGRPBEN
----- --------------------
11 Se.tj heltid tillsv.
12 Se.tj deltid tillsv.
13 Semtj hel an tj
14 Semtj del an tj
15 Uppeh.tj hel tillsv.
Both columns are padded with spaces, not tabs.
The desired output would be something like this:
DGGRP;DGGRPBEN
11;Se.tj heltid tillsv.
12;Se.tj deltid tillsv.
13;Semtj hel an tj
14;Semtj del an tj
15;Uppeh.tj hel tillsv.
It’s no problem skipping the first lines and trimming the ends of the lines, but for some reason I’m not getting my thinking right when it comes to separating the two columns.
It seems that the width of the columns are set by the widest element.
My first thought was going with something like this:
$Text = Get-Content -Path ExportFile.txt | Select-Object -Skip 2 | ForEach-Object {
$_.Trim()
}
$Text = $Text -replace('\s{2}',';')
I was thinking that would replace every instance of more than one whitespace with a semicolon, but instead I just got double semicolons on each line except for the first two with the header and the dashes as they only contain one whitespace.
I’m probably overthinking this… I tend to do that, but I’m not really getting anywhere at the moment.
I also need to remove the second line with the dashes. I can probably brute force it by ripping out the first lines and just adding the headers line back in, but if there’s a better or more elegant solution I’m all ears.
