Find a certain number in a txt file ?

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
doa
Newbie
Posts: 7
Joined: Thu Sep 20, 2012 4:25 pm

Find a certain number in a txt file ?

Post by doa » Tue Sep 25, 2012 9:44 am

Hello all,

how to find a number in a lines (I used “Separate>file1,CRLF,linesâ€

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Sep 25, 2012 11:01 am

You could read in the file, then use RegEx to find a list of matches:

Code: Select all

ReadFile>c:\bla\somefile.txt,fileData
RegEx>\d{9,10},fileData,0,matches,nm,0
//do we have a match?
If>nm>0
  //first match is in matches_1
  MessageModal>match: %matches_1%
Endif
\d{9,10} means find numeric digits repeated between 9 and 10 times (i.e. find numbers 9 or 10 digits long)

You'll get an array of matches, so if there should be more than one you can loop through the array and do as you please with is.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

doa
Newbie
Posts: 7
Joined: Thu Sep 20, 2012 4:25 pm

ok, but

Post by doa » Tue Sep 25, 2012 12:43 pm

Thank you, Marcus, but I am trying something like this below:

Code: Select all

// script should read somelog.txt and if in it has some number from numbers.txt, show a message
// in numbers.txt are just clean numbers, 9 or 10 digits long, one number per line

ReadFile>c:\log\somelog.txt,file1
ReadFIle>c:\data\numbers.txt,file2


Separate>file1,CRLF,lines


If>lines_count>0
  
  Let>k=0
  Repeat>k
    Let>k=k+1
    
    Let>this_line=lines_%k%
        
    // regex is problem...
    RegEx>%this_line%\d{9,10},file2,0,matches,nm,0
   
    
        If>nm>0
        MessageModal>Found this number, %this_line%       
         
    Endif         
     
  Until>k=lines_count
Endif

I hope you can now see what I need to do.
Thanks again

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Sep 25, 2012 12:48 pm

This shows me that you are reading a file and then you loop through each line, and you use the regex pattern I provided to search each line to see if it contains a 9-10 digit number. If any found it pops the first one in a message box. The code does nothing with the second file.

That's what your code does. But it doesn't tell me what you need to do.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

doa
Newbie
Posts: 7
Joined: Thu Sep 20, 2012 4:25 pm

Post by doa » Tue Sep 25, 2012 5:14 pm

Ok, to be more specific, a part of log file is:
9/10/12 3:18PM 03 7654751399 ....
9/20/12 3:21PM 01 624226422 ....
9/10/12 3:21PM 02 7697137935 ....
9/10/12 3:21PM 101 01 514226746 00:00'16" ....
9/10/12 3:21PM 101 02 769713793 00:00'32" ....
9/10/12 3:23PM 03 514239008 ....
9/20/12 3:24PM 01 514226422 ....
Part of second file, numbers.txt
514222312
542235735
7673125253
514256188
And script should read somelog.txt in real time and if in somelog.txt exist some number(s) from numbers.txt, just to show a message with that found number.

What shuld be the right regex for that ? I am confused with that all.
Thank you Marcus and all the best

doa
Newbie
Posts: 7
Joined: Thu Sep 20, 2012 4:25 pm

Post by doa » Wed Sep 26, 2012 8:32 am

I did it on this way, actually I need only the last line from log file
:lol:

Code: Select all

ReadFile>c:\log\somelog.txt,data
Separate>data,CRLF,lines
Let>last_line=%lines_count%
ReadLn>c:\log\somelog.txt,%last_line%,last_one

Let>pattern=\d{9,10}
RegEx>pattern,%last_one%,0,matches,num,0

ReadFile>c:\data\numbers.txt,file2

RegEx>(?m)^%matches_1%\d*$,file2,0,matches,nm,0
    
        If>nm>0

               MessageModal>Found it, %matches_1%
       
         
        Endif

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts