views:

508

answers:

5

Hi all,

I was just wondering if anyone knew of a good way that I could parse the file at the bottom of the post.

I have a database setup with the correct tables for each section eg Refferal Table,Caller Table,Location Table. Each table has the same columns that are show in the file below

I would really like something that is fairly genetic so if the file layout changes it won't mess me around to much. At the moment I am just reading the file in a line at a time and just using a case statement to check which section i'm in.

Is anyone able to help me with this?

PS. I am using VB but C# or anything else will be fine, also the x's in the document are just personal info I have blanked

Thanks, Nathan

File:--->

DIAL BEFORE YOU DIG
Call 1100, Fax 1300 652 077
PO Box 7710 MELBOURNE, VIC 8004

Utilities are requested to respond within 2 working days and reference the Sequence number.

[REFFERAL DETAILS]
FROM=                 Dial Before You Dig - Web
TO=                   Technical Services
UTILITY ID=           xxxxxx
COMPANY=              {Company Name}
ENQUIRY DATE=         02/10/2008 13:53
COMMENCEMENT DATE=    06/10/2008
SEQUENCE NO=          xxxxxxxxx
PLANNING=             No

[CALLER DETAILS]
CUSTOMER ID=          403552
CONTACT NAME=         {Name of Contact}
CONTACT HOURS=        0
COMPANY=              Underground Utility Locating
ADDRESS=              {Address}
SUBURB=               {Suburb}
STATE=                {State}
POSTCODE=             4350
TELEPHONE=            xxxxxxxxxx
MOBILE=               xxxxxxxxxx
FAX TYPE=             Private
FAX NUMBER=           xxxxxxxxxx
PUBLIC ADDRESS=       xxxxxxxxxx
PUBLIC TELEPHONE=
EMAIL ADDRESS=        {Email Address}

[LOCATION DETAILS]
ADDRESS=              {Location Address}
SUBURB=               {Location Suburb}
STATE=                xxx
POSTCODE=             xxx
DEPOSITED PLAN NO=    0
SECTION & HUNDRED NO= 0
PROPERTY PHONE NO=
SIDE OF STREET=       B
INTERSECTION=         xxxxxx
DISTANCE=             0-200m B
ACTIVITY CODE=        15
ACTIVITY DESCRIPTION= xxxxxxxxxxxxxxxxxx
MAP TYPE=             StateGrid
MAP REF=              Q851_63
MAP PAGE=
MAP GRID 1=
MAP GRID 2=
MAP GRID 3=
MAP GRID 4=
MAP GRID 5=
GPS X COORD=
GPS Y COORD=
PRIVATE/ROAD/BOTH=    B
TRAFFIC AFFECTED=     No
NOTIFICATION NO=      3082321
MESSAGE=              entire intersection of Allora-Clifton rd , Hillside
rd and merivale st

MOCSMESSAGE=          Digsafe generated referral

Notice: Please DO NOT REPLY TO THIS EMAIL as it has been automatically generated and replies are not monitored. Should you wish to advise Dial Before You Dig of any issues with this enquiry, please Call 1100

(See attached file: 3082321_LLGDA94.GML)
+5  A: 

Google has the answers, once you know that the file-format is called '.ini'

Edit: That is, it's an .ini plus some extra leading/trailing gunk.

It looks like a .ini, with the exception of the last line, that appears to be just unstructured text.
Sergio Acosta
Oh, and the first four lines.
Sergio Acosta
True, it does look like it needs to be trimmed of leading/trailing gunk.
It comes via email in the body, so no its not an ini file even though it looks like one :)
Nathan W
grep for /\=|^\[.*\]$/ first should get rid of the junk, right?
Simon Buchan
@Nathan: Umm, I'm not saying it's filename is "something.ini", I'm saying it follows the .ini format and therefore can be parsed by an .ini parser.
@Mike yeah I know. Sorry about my comment. Having a bad day so not really thinking straight
Nathan W
+4  A: 

You could read each line of the file sequentially. Each line is essentially a name value pair. Place each value in a map (hash table) keyed by name. Use a map for each section. When done parsing the file you'll have maps containing all the name value pairs. Iterate over each map and populate your database tables.

Steve Kuo
+2  A: 

Will do your job for money ;-)

VVS
+2  A: 
pbh101
You are missing the fact that some 'columns' occur more than once in a different 'context'. For example POSTCODE occurs in both the [CALLER DETAILS] and [LOCATION DETAILS] sections.
GerG
True, but that could be solved by making different dictionaries for the different tables in which to insert, which, I think, would be wise anyway.
pbh101
+1  A: 
Using f As StreamReader = File.OpenText("sample.txt")
    Dim g As String = "undefined"
    Do
        Dim s As String = f.ReadLine
        If s Is Nothing Then Exit Do
        s = s.Replace(Chr(9), " ")
        If s.StartsWith("[") And s.EndsWith("]") Then
            g = s.Substring("[".Length, s.Length - "[]".Length)
        Else
            Dim ss() As String = s.Split(New Char() {"="c}, 2)
            If ss.Length = 2 Then
                Console.WriteLine("{0}.{1}={2}", g, Trim(ss(0)), Trim(ss(1)))
            End If
        End If
    Loop
End Using
Hafthor