Jump to content

Find a partial string in a file and retrieve the entire line


Recommended Posts

Hi all,

I have a file which contains data in the following order:

file.txt:

=====

192.168.1.1 || root

192.168.1.2 || admin

....etc

my search is through a gui, based on the IP address. so basically when a user enters 192.168.1.1. I need to find that string in this file.txt and then

retrieve that line, and then split it and retrieve the "root" string.

I am not sure of a method that would help me retrieve the line from a file based on a search string.

Any help would be greatly appreciated.

Thanks,

Kris

Link to comment
Share on other sites

Hi all,

I have a file which contains data in the following order:

file.txt:

=====

192.168.1.1 || root

192.168.1.2 || admin

....etc

my search is through a gui, based on the IP address. so basically when a user enters 192.168.1.1. I need to find that string in this file.txt and then

retrieve that line, and then split it and retrieve the "root" string.

I am not sure of a method that would help me retrieve the line from a file based on a search string.

Any help would be greatly appreciated.

Thanks,

Kris

If there is a way to get the line number based on a partial string search, then i can use the filereadline method to get the entire line.

Link to comment
Share on other sites

#include <Array.au3>

$string = '192.168.1.1 || root' & @CRLF & '192.168.2.2 || admin' & @CRLF & '192.168.3.1 || root'

MsgBox(0,0, StringStripCR(_getName($string, '192.168.2.2')))

Func _getName($string, $suche)
    Local $re = StringRegExp($string, $suche & '\s*\|\|\s*(.*)', 3)
    Return $re[0]
EndFunc  ;==>_getName

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

#include <Array.au3>

$string = '192.168.1.1 || root' & @CRLF & '192.168.2.2 || admin' & @CRLF & '192.168.3.1 || root'

MsgBox(0,0, StringStripCR(_getName($string, '192.168.2.2')))

Func _getName($string, $suche)
    Local $re = StringRegExp($string, $suche & '\s*\|\|\s*(.*)', 3)
    Return $re[0]
EndFunc ;==>_getName

its going to be a file with endless IP addresses || login names, I cannot hardcode it.

Is there no way to find a particular string in a file and return the line number(first occurence)where it was found?

Link to comment
Share on other sites

its going to be a file with endless IP addresses || login names, I cannot hardcode it.

Is there no way to find a particular string in a file and return the line number(first occurence)where it was found?

We need some effort on your part...that was just a demonstration.

$string - FileRead("somefile.txt")

Link to comment
Share on other sites

We need some effort on your part...that was just a demonstration.

$string - FileRead("somefile.txt")

heres an example snippet of wht i have.

$find = "192.168.1.1" ;assuming GUI returned this value

$string = FileRead("somefile.txt") ;String now has the entire file contents

$ret = StringInStr($string, $find) ; this only will give the position of the substring where it was found.

what i need is the linenumber .....

Link to comment
Share on other sites

In my hurry, i never saw xenobiologist's reply, heres wht I have, i get a "subscript used with non-array variable" exception

heres wht i have :

============

$remip = guictrlread($ip1)

$passfile = Fileopen("somtext.txt", 0)

$string = fileread($passfile)

MsgBox(0,0, StringStripCR(_getName($string, $remip)))

Func _getName($string, $suche)

Local $re = StringRegExp($string, $suche & '\s*\|\|\s*(.*)', 3)

Return $re[0]

EndFunc ;==>_getName

wheres the mistake?

Link to comment
Share on other sites

In my hurry, i never saw xenobiologist's reply, heres wht I have, i get a "subscript used with non-array variable" exception

heres wht i have :

============

$remip = guictrlread($ip1)

$passfile = Fileopen("somtext.txt", 0)

$string = fileread($passfile)

MsgBox(0,0, StringStripCR(_getName($string, $remip)))

Func _getName($string, $suche)

Local $re = StringRegExp($string, $suche & '\s*\|\|\s*(.*)', 3)

Return $re[0]

EndFunc ;==>_getName

wheres the mistake?

If you didn't get an array back from StringRegExp() you can check @error and @extended to find out why (see the help file). Since the example posted by xenobiologist works fine, the problem is with the data you are passing to it.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

If you didn't get an array back from StringRegExp() you can check @error and @extended to find out why (see the help file). Since the example posted by xenobiologist works fine, the problem is with the data you are passing to it.

muttley

I did solve the problem , heres wht i did.

$newip = "192.168.1.1"

For $x = 1 to $array1[0]

If StringInStr($array1[$X], $newip) Then

$passline=FileReadLine($filepass, $x)

$passrem = StringSplit($passline,",")

$asspas = $passrem[2]

fileclose($filepass)

$filepass ="sometext.txt"

$newline = StringReplace($passline,$asspas,$newp)

$su = _ReplaceStringInFile($filepass,$passline,$newline)

fileclose($filepass)

EndIf

Next

The subscript error because I had opened the file in write mode, was trying to read and write at the same time, hence the anomaly.

Thanks a lot everyone. :)!!

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...