Sign in to follow this  
Followers 0
Zingzi

comparing data from two .txt files

8 posts in this topic

Hello folks,

I am a complete noob at this so forgive up front please.

I have been messing around AutoIT the last few weeks and it has been so much fun! I'm hooked. I needed something light and portable and this fits the bill perfectly. Right now I am using it to gleam data from my network and splitting the data down into two ":" delimited txt files. All data is alphanumeric.

The first txt file contains a data structure like this and is around 400 lines long:

data1:data2

The second txt file contains a data structure like this and also is around 400 lines long:

data2:data3

What I am trying to do, and failing at, is searching the first txt file for data1 to get data2 and then searching the second txt file using data2 to find data3. The end result would be a parsed txt file containing data1:data2:data3.

I am not asking anyone to do this for me but rather point this noob in the right direction.

Thanks

Share this post


Link to post
Share on other sites



Welcome to the forum,

That's a perfect job for two scripting dictionnaries (use forum's search feature).


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Zingzi,

Welcome to the AutoIt forum. :oops:

Nice attitude there - it will help you a lot here! :D

I would start by reading the files into arrays using _FileReadToArray. Then you can loop through the first file in a For...Next loop using StringInStr to detect the presence of "data1". if it is there you can extract "data2" in a number of ways - perhaps using StringSplit.

Once you have "data2" you repeat the process on the second file to find "data3".

Then you concatenate the 3 bits of data and use FileWrite to add it to a new file. :)

Give it a try - you know where we are if you run into difficulties. :rip:

M23

Edit: Or you can try jchd's idea. :D

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Thanks for the kick in the right direction M23. :D

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

If your files are using delimitered strings you might want to use a two dimensional array. I recently wrote a script which you might find useful. You will need to run the code below. Although it's intended for csv you can set the delimiter to whatever you like.

#include <Array.au3> ; For array display purposes
#include <CSVSplit.au3>
 
Dim $file = FileOpen("test.txt", 0)
If $file = -1 Then ; Check if file opened for reading OK
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
 
Dim $sDataString = FileRead($file)
FileClose($file)
 
Dim $aArray = _CSVSplit($sDataString, ":") ; Using colon delimiter
_ArrayDisplay($aArray)

Once your data is organized in an array you can loop through each row, and then each column using two nested For loops.

Word of warning, the only problem that could arise is if your file contains double quote characters, since the script was originally designed to be used with csv.

Edit : just fixed an error in the code

Edited by czardas

Share this post


Link to post
Share on other sites

I apologize for laconic post above (I was a little upset with unreliable 3G link that trashed typed text 3 times).


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

no worries there jchd. :D

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