Go Premium for a chance to win a PS4. Enter to Win

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 124

# Importing invoice lines in an array in powershell

Hello,

I need to script the import of invoices into an array.
The problem is that the "table" is not always the same....
I have been trying to do it manually with notepad and just with find and replace create char delimited file.
The only way I see it working is to start at the end:

so first put the "SR" or "OS" in a column, then the total price if it exists, then the price, then the amount, then the description and then the partnumber.

I have no idea if it is possible due to the bad source...

so my output would be 6 columns with data

THanks
invoice-lines.txt
0
Hans de Jongh
• 4
• 4
• 3
• +1
2 Solutions

Systems EngineerCommented:
Could adjust the data source?  What I mean is, could the rows with the 2 missing columns, have "0" entered if no entry exists.  If the data is coming out of a database, can the 2 fields in the table be defaulted to have an automatic entry of "0" when a record is created?

This would make every row have 6 columns.  If make it only a little easier to import.

Otherwise... You could read in a line and split it on the separator 'space'.  Then trim the whitespace, so there are no extra blank characters on the field.  Check to see if the split destination array has either 4 or 6 elements, if there are 6 elements, just copy that data into a new array.  If the array has 4 elements, move the data into the new array but add the missing fields and define them as "0".

Now you'll have the final array with 6 elements, and all values are populated.

Dan
0

Author Commented:
hi,

no these are converted pdf's. so no adjusting possible.
No I thought about that aswell but the problem is that sometimes in the text you find whitepaces of 6 elements so that doesn't work either...
0

Commented:
Regex is very powerful.  Luckily there appears to be enough of a pattern to match here.  It was a bit of a pain that the description can include a block of spaces.  Anyway, here's what I have.
$pattern = "(?<part>^[0-9]{3}-[a-z0-9]{4,5}) +(?<desc>.+?) {7,}(?<qty>[0-9]{1,3}) +(?:(?<price>[0-9.,]+) +(?<total>[0-9.,]+) +)?(?<code>(SR|OS))$"
Get-Content .\temp\invoice-lines.txt | Where {$_ -match$pattern} | ForEach 
{
New-Object PsObject -Property ([ordered]@{
Part = $Matches.part Description =$Matches.Desc
Quantity = [int32]$Matches.qty Price =$Matches.price
Total = $Matches.total Code =$Matches.code
})
}

I might even replace line 6 with the following to remove the blocks of spaces from the description.
                    Description = $Matches.Desc -replace " {2,}"," "  0 Systems EngineerCommented: So here's a partial solution. The code below reads the file into a variable, then goes thru each line and replaces anything that match 2 or more space with a pipe. $SourceData = gc source.txt -totalcount 10
foreach ($row in$SourceData)
{
$row = [regex]::replace($row,"\b[ ]{2,}\b",'|')
$row }  This leaves 6 lines with an issue because there is only 1 space between the first field and second. Dan 0 Systems EngineerCommented: The example above is for test and only looks at the first 10 lines. Limits the data for testing. Only thing left is to try to find lines with only 1 space between the first field and second. Knowing that the second field can contain a phrase that may contain single spaces between words. Dan 0 C++ DeveloperCommented: Similar solution as footech, who had to post while I was assembling something @*#. Doesn't rely on PowerShell 4: cls Get-Content c:\temp\ee\invoice-lines.txt | ? {$_ -match '(?<PartNo>\S*)\s+(?<Description>.*)\s\s+(?<Amount>\d+)\s+(?<Prize>[0-9.]+,\d\d)?\s+(?<Total>[0-9.]+,\d\d)?\s+(?<SR>..)'} |
% {
New-Object PsObject -Property @{
PartNo      = $matches.PartNo Description =$matches.Description
Amount      = $matches.Amount Prize =$matches.Prize
Total       = $matches.Total SR =$matches.SR
}
} |
select PartNo, Description, Amount, Prize, Total, SR | ft -AutoSize
`
0

Author Commented:
thanks everybody, I guess I give the points to the first who solved this? (sorry)
0

Author Commented:
check 10000 lines and not a single failure!
0

Commented:
:)
I find it a bit interesting to see the differing regex patterns that others use to parse the same input.

I know Qlemo knows this, but for others who may not, you can adjust mine so that it doesn't rely on PowerShell 3+ (not just 4) just by removing [ordered] from line 4.  You would then have a use a Select statement like Qlemo did to order the columns if so desired.
0

Commented:
It's a judgement call.  When you have multiple working solutions posted at about the same time, I think it's appropriate to split points.  As a topic adviser, Qlemo could probably advise you better on this than I.
0

Systems EngineerCommented:
Interesting parsing challenge!  Glad the issue was resolved and I get to take away a nice regex lesson.

Dan
0

Author Commented:
so how can I get back the points and give them away (split them)
in a 70/30 split
0

## Featured Post

• 4
• 4
• 3
• +1
Tackle projects and never again get stuck behind a technical roadblock.