![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | How to merge teh lines of two text files? Assume I have two text files with multiple (long) lines each. Now I want to merge them insofar that duplicate parts are eliminated and the remaining parts are merged. Say I have the following files: ---- file1.txt ---- a aaa bbbb cccc dddd ------------------- ---- file2.txt ---- cccc dddd eee ff fff ------------------- Then the resulting file should look like: ---- result.txt --- a aaa bbbb cccc dddd eee ff fff ------------------- How can I code this with VB? Keep in mind that there can be various combinations. E.g. file2.txt could contain only a subset of lines: ---- file2b.txt --- bbbb cccc ------------------- Then the result would be the file1.txt Or - next variant - lines of file2.txt could overlap in front of file1.txt like ---- file2c.txt --- kkk kkk zz zzzzzz a aaa bbbb ------------------- Would result in a resultfile ---- result3.txt ---- kkk kkk zz zzzzzz a aaa bbbb cccc dddd --------------------- Again: How can I code such a merge textfiles utility? The vbscript should later be callable from commandline like mymergetool.vb file1.txt file2.txt >result.txt Tony |
My System Specs![]() |
| | #2 (permalink) |
| | Re: How to merge teh lines of two text files? "Tony Bansten" <tonytony@xxxxxx> wrote in message news:499d8c61$0$32674$9b4e6d93@xxxxxx-online.net... Quote: > Assume I have two text files with multiple (long) lines each. > Now I want to merge them insofar that duplicate parts are eliminated > and the remaining parts are merged. Say I have the following files: > > ---- file1.txt ---- > a aaa > bbbb > cccc > dddd > ------------------- > > ---- file2.txt ---- > cccc > dddd > eee > ff fff > ------------------- > > Then the resulting file should look like: > > ---- result.txt --- > a aaa > bbbb > cccc > dddd > eee > ff fff > ------------------- > > How can I code this with VB? > > Keep in mind that there can be various combinations. > E.g. file2.txt could contain only a subset of lines: > > ---- file2b.txt --- > bbbb > cccc > ------------------- > > Then the result would be the file1.txt > > Or - next variant - lines of file2.txt could overlap in front of file1.txt > like > > ---- file2c.txt --- > kkk kkk > zz zzzzzz > a aaa > bbbb > ------------------- > > Would result in a resultfile > > ---- result3.txt ---- > kkk kkk > zz zzzzzz > a aaa > bbbb > cccc > dddd > --------------------- > > Again: How can I code such a merge textfiles utility? > > The vbscript should later be callable from commandline like > > mymergetool.vb file1.txt file2.txt >result.txt > > > Tony > track of unique lines in a dictionary object. For example: ============ Option Explicit Dim objFSO, strFile1, strFile2, objFile1, objFile2 Dim objLines, strLine Const ForReading = 1 ' Check for two parameters, the names of files. If (Wscript.Arguments.Count <> 2) Then Wscript.Echo "Two file names required" Wscript.Quit End If strFile1 = Wscript.Arguments(0) strFile2 = Wscript.Arguments(1) ' Open the two files for read access. Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile1 = objFSO.OpenTextFile(strFile1, ForReading) Set objFile2 = objFSO.OpenTextFile(strFile2, ForReading) ' Create dictionary object. Set objLines = CreateObject("Scripting.Dictionary") ' Make comparisons case insensitive. objLines.CompareMode = vbTextCompare ' Read each line of the first file. Do Until objFile1.AtEndOfStream strLine = Trim(objFile1.ReadLine) ' Skip blank lines. If (strLine <> "") Then ' Only add unique lines to the dictionary object. If (objLines.Exists(strLine) = False) Then objLines.Add strLine, True End If End If Loop objFile1.Close ' Read each line of the second file. Do Until objFile2.AtEndOfStream strLine = Trim(objFile2.ReadLine) ' Skip blank lines. If (strLine <> "") Then ' Only add unique lines to the dictionary object. If (objLines.Exists(strLine) = False) Then objLines.Add strLine, True End If End If Loop objFile2.Close ' Output unique lines. For Each strLine in objLines.Keys Wscript.Echo strLine Next ======= You can redirect the output to a text file, as you suggested. If either file is not in the current directory, include the full path to the file. -- Richard Mueller MVP Directory Services Hilltop Lab - http://www.rlmueller.net -- |
My System Specs![]() |
| | #3 (permalink) |
| | Re: How to merge teh lines of two text files? "Tony Bansten" <tonytony@xxxxxx> wrote in message news:499d8c61$0$32674$9b4e6d93@xxxxxx-online.net... Quote: > Assume I have two text files with multiple (long) lines each. > Now I want to merge them insofar that duplicate parts are eliminated > and the remaining parts are merged. Say I have the following files: > > ---- file1.txt ---- > a aaa > bbbb > cccc > dddd > ------------------- > > ---- file2.txt ---- > cccc > dddd > eee > ff fff > ------------------- > > Then the resulting file should look like: > > ---- result.txt --- > a aaa > bbbb > cccc > dddd > eee > ff fff > ------------------- > > How can I code this with VB? > > Keep in mind that there can be various combinations. > E.g. file2.txt could contain only a subset of lines: > > ---- file2b.txt --- > bbbb > cccc > ------------------- > > Then the result would be the file1.txt > > Or - next variant - lines of file2.txt could overlap in front of file1.txt > like > > ---- file2c.txt --- > kkk kkk > zz zzzzzz > a aaa > bbbb > ------------------- > > Would result in a resultfile > > ---- result3.txt ---- > kkk kkk > zz zzzzzz > a aaa > bbbb > cccc > dddd > --------------------- > > Again: How can I code such a merge textfiles utility? > > The vbscript should later be callable from commandline like > > mymergetool.vb file1.txt file2.txt >result.txt > > > Tony > It appears from your example that the output order is not important. If that is correct, just read from both files, putting the individual lines into an expanding list, first verifying the item is not in the list. At the end, the list contains all unique strings from both files. If the output order is important, than you need to explain the output order scheme. -- Mike |
My System Specs![]() |
| | #4 (permalink) |
| | Re: How to merge teh lines of two text files? On Feb 19, 10:44*am, tonyt...@xxxxxx (Tony Bansten) wrote: Quote: > Assume I have two text files with multiple (long) lines each. > Now I want to merge them insofar that duplicate parts are eliminated > and the remaining parts are merged. Say I have the following files: > > ---- file1.txt ---- > a aaa > bbbb > cccc > dddd > ------------------- > > ---- file2.txt ---- > cccc > dddd > eee > ff fff > ------------------- > > Then the resulting file should look like: > > ---- result.txt --- > a aaa > bbbb > cccc > dddd > eee > ff fff > ------------------- > > How can I code this with VB? > > Keep in mind that there can be various combinations. > E.g. file2.txt could contain only a subset of lines: > > ---- file2b.txt --- > bbbb > cccc > ------------------- > > Then the result would be the file1.txt > > Or - next variant - lines of file2.txt could overlap in front of file1.txt like > > ---- file2c.txt --- > kkk kkk > zz zzzzzz > a aaa > bbbb > ------------------- > > Would result in a resultfile > > ---- result3.txt ---- > kkk kkk > zz zzzzzz > a aaa > bbbb > cccc > dddd > --------------------- > > Again: How can I code such a merge textfiles utility? > > The vbscript should later be callable from commandline like > > mymergetool.vb file1.txt file2.txt >result.txt > > Tony Here's some pseudo-code to get you started. Assumption: The entire line in each file should be compared. (1) Read file #1 into a string array line by line - call it Array1 (2) Read file #2 into a string array line by line - call it Array2 (3) In a loop compare a line in Array1 to every line in Array2. If it is not found, write it to a new array (Array3). (4) In a loop compare a line in Array2 to every line in Array1. If it is not found, write it to a new array (Array3). (5) Save contents of Array3 to a new file. |
My System Specs![]() |
| | #5 (permalink) |
| | Re: How to merge teh lines of two text files? This is the merge part of a standard merge-sort. Read first line of each file Loop If the two lines are the same, write out either one and read a new one from each input file If they are different, write out the lower-sequenced one and read a line from the corresponding input file to replace it Repeat When one file ends, copy the remainder of the other to the output file. "Tony Bansten" <tonytony@xxxxxx> wrote in message news:499d8c61$0$32674$9b4e6d93@xxxxxx-online.net... Quote: > Assume I have two text files with multiple (long) lines each. > Now I want to merge them insofar that duplicate parts are eliminated > and the remaining parts are merged. Say I have the following files: > > ---- file1.txt ---- > a aaa > bbbb > cccc > dddd > ------------------- > > ---- file2.txt ---- > cccc > dddd > eee > ff fff > ------------------- > > Then the resulting file should look like: > > ---- result.txt --- > a aaa > bbbb > cccc > dddd > eee > ff fff > ------------------- > > How can I code this with VB? > > Keep in mind that there can be various combinations. > E.g. file2.txt could contain only a subset of lines: > > ---- file2b.txt --- > bbbb > cccc > ------------------- > > Then the result would be the file1.txt > > Or - next variant - lines of file2.txt could overlap in front of file1.txt > like > > ---- file2c.txt --- > kkk kkk > zz zzzzzz > a aaa > bbbb > ------------------- > > Would result in a resultfile > > ---- result3.txt ---- > kkk kkk > zz zzzzzz > a aaa > bbbb > cccc > dddd > --------------------- > > Again: How can I code such a merge textfiles utility? > > The vbscript should later be callable from commandline like > > mymergetool.vb file1.txt file2.txt >result.txt > > > Tony > |
My System Specs![]() |
| | #6 (permalink) |
| | Re: How to merge teh lines of two text files? Hi , Have you got the answer for it. If yes, please do send it me. Thanks, Veera. Mundra "Tony Bansten" <tonytony@xxxxxx> wrote in message news:499d8c61$0$32674$9b4e6d93@xxxxxx-online.net... Quote: > Assume I have two text files with multiple (long) lines each. > Now I want to merge them insofar that duplicate parts are eliminated > and the remaining parts are merged. Say I have the following files: > > ---- file1.txt ---- > a aaa > bbbb > cccc > dddd > ------------------- > > ---- file2.txt ---- > cccc > dddd > eee > ff fff > ------------------- > > Then the resulting file should look like: > > ---- result.txt --- > a aaa > bbbb > cccc > dddd > eee > ff fff > ------------------- > > How can I code this with VB? > > Keep in mind that there can be various combinations. > E.g. file2.txt could contain only a subset of lines: > > ---- file2b.txt --- > bbbb > cccc > ------------------- > > Then the result would be the file1.txt > > Or - next variant - lines of file2.txt could overlap in front of file1.txt > like > > ---- file2c.txt --- > kkk kkk > zz zzzzzz > a aaa > bbbb > ------------------- > > Would result in a resultfile > > ---- result3.txt ---- > kkk kkk > zz zzzzzz > a aaa > bbbb > cccc > dddd > --------------------- > > Again: How can I code such a merge textfiles utility? > > The vbscript should later be callable from commandline like > > mymergetool.vb file1.txt file2.txt >result.txt > > > Tony > |
My System Specs![]() |
| | #7 (permalink) |
| | Re: How to merge teh lines of two text files? On Feb 19, 12:08*pm, "Richard Mueller [MVP]" <rlmueller- nos...@xxxxxx> wrote: Quote: > "Tony Bansten" <tonyt...@xxxxxx> wrote in message > > news:499d8c61$0$32674$9b4e6d93@xxxxxx-online.net... > > > Quote: > > Assume I have two text files with multiple (long) lines each. > > Now I want to merge them insofar that duplicate parts are eliminated > > and the remaining parts are merged. Say I have the following files: Quote: > > ---- file1.txt ---- > > a aaa > > bbbb > > cccc > > dddd > > ------------------- Quote: > > ---- file2.txt ---- > > cccc > > dddd > > eee > > ff fff > > ------------------- Quote: > > Then the resulting file should look like: Quote: > > ---- result.txt --- > > a aaa > > bbbb > > cccc > > dddd > > eee > > ff fff > > ------------------- Quote: > > How can I code this with VB? Quote: > > Keep in mind that there can be various combinations. > > E.g. file2.txt could contain only a subset of lines: Quote: > > ---- file2b.txt --- > > bbbb > > cccc > > ------------------- Quote: > > Then the result would be the file1.txt Quote: > > Or - next variant - lines of file2.txt could overlap in front of file1.txt > > like Quote: > > ---- file2c.txt --- > > kkk kkk > > zz zzzzzz > > a aaa > > bbbb > > ------------------- Quote: > > Would result in a resultfile Quote: > > ---- result3.txt ---- > > kkk kkk > > zz zzzzzz > > a aaa > > bbbb > > cccc > > dddd > > --------------------- Quote: > > Again: How can I code such a merge textfiles utility? Quote: > > The vbscript should later be callable from commandline like Quote: > > mymergetool.vb file1.txt file2.txt >result.txt Quote: > > Tony > I would use the FileSystemObject to read the lines of the files and keep > track of unique lines in a dictionary object. For example: Quote: > You can redirect the output to a text file, as you suggested. If either file > is not in the current directory, include the full path to the file. > > -- > Richard Mueller > MVP Directory Services > Hilltop Lab -http://www.rlmueller.net > -- is ... Option Explicit Dim strName, strLine, arrLines, arrList Const ForReading = 1 ' Check for two parameters, the names of files. If (Wscript.Arguments.Count <> 2) Then wsh.Stderr.Writeln "Two file names required" Wscript.Quit End If ' List object available to scripting from .Net framework Set arrList = CreateObject("System.Collections.ArrayList") ' Instantiate FSO with CreateObject("Scripting.FileSystemObject") ' Open each file in turn for read access. For each strName in Wscript.Arguments if .FileExists(strName) Then with .OpenTextFile(strName, ForReading) arrLines = Split(.ReadAll, vbNewline) for each strline in arrLines if not arrList.contains(trim(strLine)) Then arrList.Add trim(strLine) end if next ' strline end with ' file else wsh.Stderr.Writeln "File not found: " & sName wsh.quit end if next ' name end with ' FSO ' Sort (optional) arrList.Sort() 'arrList.Reverse() ' use this and the last shall be first For Each strLine in arrList wsh.echo strLine Next It adds the ability to sort the result, if desired. I also sent the error messages to StdErr, rather than to StdOut, just in case. Tom Lavedas ========== |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Remove lines from multiple text files | PowerShell | |||
| Retrieving selected lines from all text files in a directory | PowerShell | |||
| Howto: Add lines of text from a specific point in a text file.. | VB Script | |||
| Suppressing blank lines in a mail merge | .NET General | |||
| Lines of text overlap | Vista General | |||