Page 1 of 1 1
Topic Options
#156932 - 2006-02-08 01:57 PM Search entire text file?
JazzM Offline
Getting the hang of it

Registered: 2004-04-23
Posts: 91
Probably I’m doing this in wrong way. Need some feedback.

Assume there is a text file called “inv.txt”
The file contains several lines; each line has three values separated by space.
The second value is our search input. (The capital letter):


    a B c
    d E f
    m N o
    g E h


Now we want to search the entire file for specific value.

Find all lines where second value is "B" and print out the first and third value for those found lines.

Code:
   
$file = “inv.txt”
$input = “B”

$ = Open (1,$file)
$Line = ReadLine (1)

If InStr($Line,$input)
$LFound = Split($Line," ")

For Each $item in $LFound

If 0 <= Ubound($LFound)
$First = $LFound[0]

If 0 <= Ubound($LFound)
$second = $LFound[1]

If 0 <= Ubound($LFound)
$third = $LFound[2]

Next
$First ?
$second ?
$third ?



Above will work if the input value is “B”.
But in case we search for “E”, then we have problem.

It seems that code will only read the first line of the file.
The other problem is when the input value is located in more then one line.
In this case the letter “E” is located in two lines.

How do I make it work in a clever way?
_________________________
I will always look out from behind these eyes...

Top
#156933 - 2006-02-08 02:10 PM Re: Search entire text file?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
How about this:
Code:
Break ON
$asSearchStrings="B","E"

If Open(1,"inv.txt")
"Cannot open file for reading ["+@ERROR+"] "+@SERROR+@CRLF
Else
$sLine=ReadLine(1)
While Not @ERROR
$asLine=Split($sLine+" ")
If 1+AScan($asSearchStrings,$asLine[1])
"Match: "+$asLine[1]+" First="+$asLine[0]+" Second="+$asLine[1]+" Third="+$asLine[2]+@CRLF
EndIf
$sLine=ReadLine(1)
Loop
EndIf



Results:
Quote:

Match: B First=a Second=B Third=c
Match: E First=d Second=E Third=f
Match: E First=g Second=E Third=h



Top
#156934 - 2006-02-08 03:10 PM Re: Search entire text file?
JazzM Offline
Getting the hang of it

Registered: 2004-04-23
Posts: 91
Thanx, but strange... no outout that all!.
Cannot figure out why
_________________________
I will always look out from behind these eyes...

Top
#156935 - 2006-02-08 03:23 PM Re: Search entire text file?
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
readfile() UDF
FilterArray() UDF

$file=readfile("inv.txt")
$filtered=filterarray($file,' B ')
for each $item in $filtered
? $item
next
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#156936 - 2006-02-08 03:29 PM Re: Search entire text file?
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
no output?
are you sure you are running the script you are modifying?
_________________________
!

download KiXnet

Top
#156937 - 2006-02-08 03:38 PM Re: Search entire text file?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Well, I just used your example data and it works fine.

How many spaces are there in your output? I've assumed that there are no spaces at the front of the line and that each field is delimited by a single space. If this is not the case then you will need to deal with the additional redundant spaces.

Top
#156938 - 2006-02-08 03:41 PM Re: Search entire text file?
JazzM Offline
Getting the hang of it

Registered: 2004-04-23
Posts: 91
Intressting. Radimus code works, BUT in opposite way!
It filters the input search string line and print out the rest of file content.

But I need the input search string line values!
_________________________
I will always look out from behind these eyes...

Top
#156939 - 2006-02-08 03:42 PM Re: Search entire text file?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Quote:

readfile() UDF
FilterArray() UDF

$file=readfile("inv.txt")
$filtered=filterarray($file,' B ')
for each $item in $filtered
? $item
next




This will return lines with "B" in fields other than field 2, which may or may not be an issue depending on the data.

Top
#156940 - 2006-02-08 03:57 PM Re: Search entire text file?
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
@Richard: if " b " has a delimiter (spaces around it) then that may not be the case

@Jazz
use the optional parameter for filterarray... $filtered=filterarray($file,' B ',1)

Sorry about that
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#156941 - 2006-02-08 04:03 PM Re: Search entire text file?
JazzM Offline
Getting the hang of it

Registered: 2004-04-23
Posts: 91
Richard, I dont get any output from you code.
There is one space between each letter in the inv.txt file.

No output!
_________________________
I will always look out from behind these eyes...

Top
#156942 - 2006-02-08 04:11 PM Re: Search entire text file?
JazzM Offline
Getting the hang of it

Registered: 2004-04-23
Posts: 91
Oh, Radimus i missed the parameter in the description of filterarry...

It's a crystal-clear woking code
_________________________
I will always look out from behind these eyes...

Top
#156943 - 2006-02-08 07:04 PM Re: Search entire text file?
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
richie's code does not work as it splits with double space.
remove the another space and it should work fine.
_________________________
!

download KiXnet

Top
#156944 - 2006-02-09 09:22 AM Re: Search entire text file?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Quote:

richie's code does not work as it splits with double space.
remove the another space and it should work fine.



Eh? No it doesn't! That's a "+", not a ","

The sample output was from a real run, so I know it works.

While I'm intrigued about the reasons it doesn't work for JazzM he has another solution which appears to satisfy him.

Top
#156945 - 2006-02-09 09:28 AM Re: Search entire text file?
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
damn, I must be blind.
you're right, as usual. sorry.
_________________________
!

download KiXnet

Top
#156946 - 2006-02-13 05:08 PM Re: Search entire text file?
JazzM Offline
Getting the hang of it

Registered: 2004-04-23
Posts: 91
Im working to adapt Radimus solution.
However i will try to determine why i cannot make Richards code work.

Thanx a lot all of u :-)
_________________________
I will always look out from behind these eyes...

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1003 anonymous users online.
Newest Members
StuTheCoder, M_Moore, BeeEm, min_seow, Audio
17884 Registered Users

Generated in 0.067 seconds in which 0.027 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org