Sign in to follow this  
Followers 0
koenp

compare text

15 posts in this topic

Hello Guys,

I wondering if you could give me some trigger/hint where to start:

I need to compare content of 2 files and a create a new file with the names (AA and BB) who are NOT in file2 --> file3

file1:

Koen

Evelyn

Emma

AA

BB

file2:

Koen 02 15

Evelyn

Emma 12

Jos

file3:

AA

BB

Many thanks ain advanced.

Best regards

Koen

Share this post


Link to post
Share on other sites



Hi koenp,

use _FileReadToArray() to read both files to different arrays. Then you can use _ArraySearch() to look for the entries of array1 in array2.

Hope that gives you a general direction. :D


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Try something like this here:

#include <File.au3>
#include <Array.au3>

Global $aFile1, $aFile2, $sFile1 = @ScriptDir & "File1.txt", $sFile2 = @ScriptDir & "File2.txt", $sFile3 = @ScriptDir & "File3.txt"
Gen_Files($sFile1, $sFile2)

_FileReadToArray($sFile1, $aFile1)
_FileReadToArray($sFile2, $aFile2)

_ArrayDiff2File($aFile1, $aFile2, $sFile3)

Func _ArrayDiff2File($a1, $a2, $sFileDiff) ;only for 1D arrays
    If Not IsArray($a1) Then Return SetError(1, 0, 0)
    If Not IsArray($a2) Then Return SetError(2, 0, 0)
    If UBound($a1, 2) > 1 Then Return SetError(3, 0, 0)
    If UBound($a2, 2) > 1 Then Return SetError(4, 0, 0)
    If UBound($a1) < 2 Then Return SetError(5, 0, 0)
    If UBound($a2) < 2 Then Return SetError(6, 0, 0)
    Local $i, $p
    _ArrayDelete($a1, 0)
    _ArrayDelete($a2, 0)
    $a1 = _ArrayUnique($a1)
    $a2 = _ArrayUnique($a2)
    For $i = 1 To $a1[0]
        $p = _ArraySearch2($a2, $a1[$i])
        If $p > 0 Then
            $a1[$i] = ""
        EndIf
    Next
    $a1[0] = ""
    $a1 = _ArrayUnique($a1)
    If $a1[1] = "" Then _ArrayDelete($a1, 1)
    _ArrayDelete($a1, 0)
    Local $hFile = FileOpen($sFileDiff, 2)
    If $hFile = -1 Then Return SetError(7, 0, 0)
    FileWrite($hFile, _ArrayToString($a1, @CRLF))
    FileClose($hFile)
;~     _ArrayDisplay($a1) ;for debugging only
    $a1 = 0
    $a2 = 0
    Return 1
EndFunc   ;==>_ArrayDiff2File

Func _ArraySearch2($a, $sElement, $bLike = True, $iStart = 0)
    Local $j
    For $j = $iStart To UBound($a) - 1
        If $bLike And StringInStr($a[$j], $sElement) And StringRight($a[$j], 1) <> @TAB Then
            Return $j
        Else
            If $a[$j] = $sElement Then Return $j
        EndIf
    Next
EndFunc   ;==>_ArraySearch2

Func Gen_Files($sFile1, $sFile2)
    Local $s1 = "Koen" & @CRLF & _
                            "Evelyn" & @CRLF & _
                            "Emma" & @CRLF & _
                            "AA" & @CRLF & _
                            "BB"
    Local $s2 = "Koen" & @TAB &  "02 15" & @CRLF & _
                            "Evelyn" & @TAB & @CRLF & _
                            "Emma 12" & @CRLF & _
                            "Jos"
    Local $hFile = FileOpen($sFile1, 2)
    FileWrite($hFile, $s1)
    FileClose($hFile)
    $hFile = FileOpen($sFile2, 2)
    FileWrite($hFile, $s2)
    FileClose($hFile)
EndFunc

Not fully tested!

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Thanks guys for all your help:

I am almost there

here the code that I untill now have:

There only thing that need to be correct is that the script only compares the first colum of the array, with other words:

It should be able to find/ignore "Koen" and "Emma" as well even with some digits after ( by the way the space between Koen and 02 is a @tab)

Many thanks for helping me ..

Best regards

Koen

file1:

Koen

Evelyn

Emma

AA

BB

file2:

Koen 02 15

Evelyn

Emma 12

Jos

#include <File.au3>
#include <Array.au3>
$file = FileOpen("C:UsersKoenDesktopFLnot_FL", 1)
Local $aArray_1 = 0, $aArray_2 = 0
_FileReadToArray("C:UsersKoenDesktopFLnewfile1.txt", $aArray_1) ; source
_FileReadToArray("C:UsersKoenDesktopFLnewfile2.txt", $aArray_2) ; look in
For $i = 1 To $aArray_1[0]
    For $j = 1 To $aArray_2[0]
        If StringStripWS($aArray_1[$i], 8) = StringStripWS($aArray_2[$j], 8) Then
            ContinueLoop 2
        EndIf
    Next
    ConsoleWrite('Not found: ' & $aArray_1[$i] & @CRLF)
; FileWriteLine($file, $aArray_1[$i] & @CRLF)
Next

Share this post


Link to post
Share on other sites

I forgot something that could help:

Is it possible to read a text file line by line, stops reading when it find a blanc space and thne continue next line.

By this I could create a new file first inorder to remove the @tab 02 15 (example)?

br,

Koen

Share this post


Link to post
Share on other sites

I update the code from post #3. Does it fit now for you?

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Hi Uez,

Thanks for this, Almost, the only thing is it should be the inverse, so the result should be the records from file1 who are not in file2.

For the rest your script looks perfect :-)

can you handle this?

thanks a lot,

Koen

Share this post


Link to post
Share on other sites

Updated the code in post #3 again.

And now?

Br,

UEZ

1 person likes this

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

RESPECT Uez, all credits are going to your, many thanks for resolving my issue -:-) !!!

Koen

Share this post


Link to post
Share on other sites

koenp,

Why did you open up a Suffice to say I feel I wasted my time in your last post.


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

Hi Guinnes,

I apologize I did not intend do this.

Best regards

Koen

Share this post


Link to post
Share on other sites

Just so you're aware for your future time here, creating duplicate topics is against the Forum Rules.


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

Hello

Can I as for one more request (variant the script you created):

Is it possible to ignore the records where there is something after the white @TAB:

So with the files below the result should be:

Evelyn

AA

BB

file1:

Koen

Evelyn

Emma

AA

BB

file2:

Koen 02 15

Evelyn

Emma 12

Jos

Is this is a hard thing to change?

Many thanks

Koen

Share this post


Link to post
Share on other sites

Well, I modified the code from post #3 again but be aware that too many exceptions will make your life harder!

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Thanks a lot Uez,

That is working perfectly..

Many thanks for all your help and efforts.

br

Koen

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0