Solved

Importing invoice lines in an array in powershell

Posted on 2014-12-12
14
111 Views
Last Modified: 2014-12-12
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
Comment
Question by:Hans de Jongh
  • 4
  • 4
  • 3
  • +1
14 Comments
 
LVL 26

Expert Comment

by:Dan McFadden
ID: 40495741
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 Comment

by:Hans de Jongh
ID: 40495755
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
 
LVL 39

Accepted Solution

by:
footech earned 350 total points
ID: 40495870
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
                    })
}

Open in new window

I might even replace line 6 with the following to remove the blocks of spaces from the description.
                    Description = $Matches.Desc -replace " {2,}"," "

Open in new window

0
 
LVL 26

Expert Comment

by:Dan McFadden
ID: 40495875
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
}

Open in new window


This leaves 6 lines with an issue because there is only 1 space between the first field and second.  

Dan
0
 
LVL 26

Expert Comment

by:Dan McFadden
ID: 40495883
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
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 150 total points
ID: 40495894
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

Open in new window

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:Hans de Jongh
ID: 40495933
thanks everybody, I guess I give the points to the first who solved this? (sorry)
0
 

Author Comment

by:Hans de Jongh
ID: 40495934
check 10000 lines and not a single failure!
0
 
LVL 39

Expert Comment

by:footech
ID: 40495948
:)
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
 
LVL 39

Expert Comment

by:footech
ID: 40495955
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
 
LVL 26

Expert Comment

by:Dan McFadden
ID: 40495956
Interesting parsing challenge!  Glad the issue was resolved and I get to take away a nice regex lesson.

Dan
0
 

Author Comment

by:Hans de Jongh
ID: 40495967
so how can I get back the points and give them away (split them)
in a 70/30 split
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Set OWA language and time zone in Exchange for individuals, all users or per database.
Synchronize a new Active Directory domain with an existing Office 365 tenant
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now