SEARCHFILE - Search a text file for matching records and extract them into an array

Top  Previous  Next

Syntax:

SEARCHFILE

filename search varname [/regex /ignorecase]

Arguments:

filename

Variable or string specifying a file to search

 

search

Variable or string specifying a pattern to look for.

 

varname

Variable name to store the results in.

Options:

/regex

Optional argument enabling search pattern to support regular expressions

 

/ignorecase

Search ignores case.

 

 

This script command is used to search a file for matches to search string. This command can serve one of 2 purposes. When used without /regex it can tell you how many times a string occurs in a file which will be stored as a number in varname. When used with the /regex option it becomes much more powerful and can be used to extract all matching records from a file into an array. In this case varname takes the following form.

 

Variable

Value

varname

number of matches

varname[n]

n is the 0 based match ( so the first match is varname[0] and the last is varname[n -1] )

varname[0][n]

n is the 0 based submatch for the first match. Submatches are covered in Regular Expressions

 

Suppose you wanted to get all the error messages out of a Robo-FTP script log file...

 

[2015-12-23 12:37:02.274000|info]Robo-FTP v3.10.0.43  SN: 021141000309

[2015-12-23 12:37:02.278000|info]Copyright © 2015 Serengeti Systems Incorporated.

[2015-12-23 12:37:02.280000|info]ALL RIGHTS RESERVED.

[2015-12-23 12:37:04.729000|info]CMD:        display mine

[2015-12-23 12:37:04.730000|error]*Variable(s) not assigned or not present. [1009]

[2015-12-23 12:37:06.390000|info]CMD:        exit

[2015-12-23 12:37:06.391000|info]*Exit Robo-FTP.

 

You could use

SEARCHFILE "Robo-FTP_Script.log" "^.*\|error](.*)$" errors /regex

 

After which

Variable

Value

errors

1

errors[0]

[2015-12-23 12:37:04.730000|error]*Variable(s) not assigned or not present. [1009]

errors[0][0]

*Variable(s) not assigned or not present. [1009]

 

Related command(s): READFILE

See also: Regular Expressions