Jump to content

searching a txt file


Recommended Posts

Hey all, I know this has been answered before on previous posts but it is still not clear to me how to exactly search a txt file for a specific word.

My goal is to search a txt file that would contain something like the following

Num_Games=2272&gamename1=007: Nightfire&gamehighs1=Highly detailed visuals and set designs; a few creative sequences; the Bond theme song.&gamelows1=Highly detailed visuals and set designs; a few creative sequences; the Bond theme song.&bottomline1=Nightfire promises intoxicating goodness, but Bond slips you a mickey.&gameissue1=February 2003, p. 60&gameurl1=http://www.pcgamer.com&gamepub1=EA&gamerating1=57&gamecategory1=First-Person Shooter&gamename2=101: The 101st Airborne in Normandy&gamehighs2=Loads of atmosphere; plenty of bang-bang; dedicated authenticity.&gamelows2=Steep learning curve; slow soldiers' movements.&bottomline2=It's a sleeper, it's a keeper -- and it's the only game in town for anyone interested in paratroop operations.&gameissue2=April '99, p. 123&gameurl2=http://www.pcgamer.com&gamepub2=Empire Interactive&gamerating2=80&gamecategory2=Wargame&gamename3=10SIX&gamehighs3=Comfortable game pace; friendly community; fresh, well-rounded game design.&gamelows3=A better manual and a more streamlined user interface could help the rookies.&bottomline3=RTS fans now have their own persisent, massively multiplayer game to enjoy.&gameissue3=July '00, p.106 &gameurl3=http://www.pcgamer.com&gamepub3=SegaSoft&gamerating3=68&gamecategory3=Strategy&gamename4=11th Hour, The&gamehighs4=Looks and sounds great (with the proper hardware).&gamelows4=Puzzles still seem unrelated to the game's story; limited innovations are not worth the performance penalty.&bottomline4=A good puzzle game; lots of patience and a nitro-fuel burning PC recommended.&gameissue4=April '96, p. 141&gameurl4=http://www.pcgamer.com&gamepub4=Virgin&gamerating4=76&gamecategory4=Puzzle&gamename5=12 O'Clock High&gamehighs5=The big campaigns pack in awesome detail.&gamelows5=Manual omits data; "awesome detail" turns into "numbing repitition."&bottomline5=It's definitely a hardcore wargame, but its depth is not likely to be surpassed in our lifetimes.&gameissue5=February '00, p. 115&gameurl5=http://www.pcgamer.com&gamepub5=Talonsoft&gamerating5=78&gamecategory5=Strategy&gamename6=1503 A.D.: The New World&gamehighs6=Draws great fun out of economic and civil management; sharp AI; improved visuals.&gamelows6=Minor bugs and graphical glitches; mission goals can trip you up in the campaign.&bottomline6=Definitely an RTS for cerebral types, but it's a welcome change from tank rushes.&gameissue6=May 2003, p. 76&gameurl6=http://www.pcgamer.com&gamepub6=EA&gamerating6=81&gamecategory6=Real-Time Strategy

In here want to be able to locate text like gamename, get the following number and repeat. So the result should be for the text gamename - 123456. Then skip 1 char, the =, and read the text until you reach a &. So for the first one it would read 007: Nightfire.

My end goal would be to pick out the information I want, write it to another file, and then just use M$ Excel to sort my data.

I already know how to write to another text file and I already know how I can code it to sort it the way I need it. I just need to know how to search the text for key words I need that will tip me off to the next set of important information.

sorry if this is a stupid question :)

Link to comment
Share on other sites

$filename = "Insert File Name Here"
$source = FileRead($filename,FileGetSize($filename))
$Count = 0
$WhatYouWantToSearchFor = "hello"

While 1
   If stringInStr($source,$WhatYouWantToSearchFor,0,$count+1) Then
      $count = $count + 1
   Else
      ExitLoop
   EndIf  
WEnd

Msgbox(0,"Count","hello was found in the file " & $count & " times.")

Small example. $Count is how many times it was find. I'm sure you can figure out what everything else does :).

Edited by Burrup

qq

Link to comment
Share on other sites

Thats great, thank you.

I need help on one more thing

$filename = "database.txt"
$filename2 = "data.txt"
$source = FileRead($filename,FileGetSize($filename))
$Count = 0
$found = FileRead($filename, 1)
$WhatYouWantToSearchFor = "gamerating"

While 1
   If stringInStr($source,$WhatYouWantToSearchFor,0,$count+1) Then
      $count = $count + 1
      FileWrite($filename2, $found)
   Else
      ExitLoop
   EndIf  
WEnd

Msgbox(0,"Count", $WhatYouWantToSearchFor & " was found in the file " & $count & " times.")

After it finds the $WhatYouWantToSearchFor I want it to write to filename 2 what it found. Where I wrote FileRead($filename, 1) is obviously where I made a mistake. How could I set it to read 1 char after the $WhatYouWantToSearchFor is found.

Link to comment
Share on other sites

Well as far as I can tell from your string you want to search, the first character after "gamerating" is just the occurance of it. I mean, in your above string, it is found 6 times. The first character after the first "gamerating" is 1. The first character after the second "gamerating" is 2, and so on and so fourth. So the 6 times it found "gamerating", they would look like.

"gamerating1"
"gamerating2"
"gamerating3"
"gamerating4"
"gamerating5"
"gamerating6"

So I don't really seeing the point of searching for what it is if you already know. But if not I'm making and example now :).

qq

Link to comment
Share on other sites

Well as far as I can tell from your string you want to search, the first character after "gamerating" is just the occurance of it. I mean, in your above string, it is found 6 times. The first character after the first "gamerating" is 1. The first character after the second "gamerating" is 2, and so on and so fourth. So the 6 times it found "gamerating", they would look like.

"gamerating1"
"gamerating2"
"gamerating3"
"gamerating4"
"gamerating5"
"gamerating6"

So I don't really seeing the point of searching for what it is if you already know. But if not I'm making and example now :).

<{POST_SNAPBACK}>

Yeah, it is just the occurance of it for that example I posted. This is not what I will be doing after I understand how to do this part. This example was just to help me to understand how I would go about doing it.
Link to comment
Share on other sites

K, Run the below. It tell you how many times it was found, what was found, and then saves it to the file "data.txt".

#include <Array.au3>

Dim $Count
$Destination = "data.txt"
$Open = FileOpen($Destination, 1)
$Source = "test.txt"
$Read_Source = FileRead($Source,FileGetSize($Source))
$WhatYouWantToSearchFor = "gamerating"
$Len = StringLen($WhatYouWantToSearchFor) 

While 1
   If StringInStr($Read_Source,$WhatYouWantToSearchFor,0,$Count + 1) Then
      $Place = StringInStr($Read_Source,$WhatYouWantToSearchFor,0,$Count + 1)
      $String = StringMid ($Read_Source, $Place + $Len, 1)
      If $Count = 0 Then
         $Entries = _ArrayCreate ($WhatYouWantToSearchFor & $String)
      Else
         _ArrayAdd ($Entries,$WhatYouWantToSearchFor & $String)
      EndIf
      FileWrite($Open, $Entries[$Count] & @CRLF)
      $Count = $Count + 1
   Else
      ExitLoop
   EndIf  
WEnd

FileClose($Open)

Msgbox(0,"Count", $WhatYouWantToSearchFor & " was found in the file " & $Count & " times.")
_ArrayDisplay ($Entries,"What was found.")
Edited by Burrup

qq

Link to comment
Share on other sites

Ahhh, Thank you sooo much, thats exactly what I needed. The data I used for example was part of a 1 meg text file with over 2400 entries. I now see what I need to do for all of my future "data mining" projects. Thank you! :)

Edited by Munden
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...