Results 1 to 3 of 3

Thread: [RESOLVED] compare between 2 text files

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    82

    Resolved [RESOLVED] compare between 2 text files

    hi every one,

    i want a code for compare between 2 text files A.txt and B.txt

    all record in file B.txt includes in file A.txt but with different value.

    the digits with color red are accounts and the digits with color blue are values.

    i want to compare between A.txt and B.txt based on accounts,when find the accounts from B.txt in A.txt copy the full record for these accounts from A.txt and print it in new file C.txt
    ===============================================
    File A.txt:
    ABC20100317201000000 -----> Header
    D1503320006980000000
    D1629040003540000000
    D6284870002570000000
    D9517420006980000000
    D3561540003540000000
    D7513590002570000000
    T0000000000000000000 -----> Trailer
    ===============================================
    File B.txt:
    ABC20100314201000000 -----> Header
    D1503320002180000000
    D1629040006930000000
    D6284870007250000000
    T0000000000000000000 -----> Trailer
    ===============================================
    the output in C.txt as the following:
    1-copy the header from A.txt and print it in C.txt
    2-copy the record for the accounts that generate from compare operation from A.txt and print it in new file C.txt
    3-copy the trailer from A.txt and print it in C.txt
    4-leave on line blank in the end of the file C.txt
    ===============================================
    ABC20100317201000000 -----> Header
    D1503320006980000000
    D1629040003540000000
    D6284870002570000000
    T0000000000000000000
    ===============================================
    i hope i get help from you.
    thank you.

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: compare between 2 text files

    This requires VS2008 or later:

    vb.net Code:
    1. Sub Process()
    2.         Dim ALines() As String = IO.File.ReadAllLines("A.txt")
    3.         Dim AHeader As String = Aggregate lines In ALines Where lines.StartsWith("ABC") Into First()
    4.         Dim ATrailer As String = Aggregate lines In ALines Where lines.StartsWith("T") Into First()
    5.         Dim Blines() As String = IO.File.ReadAllLines("B.txt")
    6.  
    7.         Dim KeysB() As String = (From accounts In BLines Where accounts.StartsWith("D") Select accounts.Substring(1, 6)).ToArray
    8.        
    9.         Dim Selected() As String = (From accounts In Alines Where KeysB.Contains(accounts.Substring(1, 6))).ToArray
    10.  
    11.         Using tw As New IO.StreamWriter("C.txt")
    12.             tw.WriteLine(AHeader)
    13.             For Each line In Selected
    14.                 tw.WriteLine(line)
    15.             Next
    16.             tw.WriteLine(ATrailer)
    17.             tw.WriteLine()
    18.             tw.Flush()
    19.             tw.Close()
    20.         End Using
    21. End Sub

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    82

    Re: compare between 2 text files

    thank you very much for helping

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width