Returning a specific line of text from a file

(imported topic written by dgaynor91)

My objective is to parse a file (“mf printer.edp”), search for a specific line of text (“ResourceName=”) and return that string of text. I’ve read through most of the posts and tried massaging similar scripts but can’t get it right to obtain the desired result. I’m thinking my approach is too simplistic. This is a sample of what I’ve tried so far and many iterations (of it) since:

Q: (exists file “C:\temp\mf printer.epp”) of it ((whose line contains “ResourceName=”) as text))

E: This expression could not be parsed.

(imported comment written by dgibson91)

try this:

Q: exists file “C:\temp\mf printer.epp” whose (exists (line of it) whose (it contains “ResourceName=”))

If any line of the file contains “ResourceName=”, then it will report true. You may want to consider “starts with” instead of “contains” if you are looking for this string at the start of a line. And if the file is case insensitive, then you should use:

Q: exists file “C:\test.txt” whose (exists (line of it) whose (it as lowercase starts with “resourcename=”))

-Daryl

(imported comment written by BenKus)

And if you want to return the rest of the string, you could write:

Q: following texts of firsts “ResourceName=” of lines whose (it starts with “ResourceName=”) of file “C:\temp\mf printer.epp”

Ben

2 Likes

(imported comment written by dgaynor91)

Thanks Daryl and Ben. I incorporated both of your suggestions and got the desired result. I’m so very appreciative of your help.

(imported comment written by khanand91)

hi there, I need to search a text file just like in the example above. Although this works fine, I’d prefer not to hard code the folder path … so pls can you advise me whats wrong with the following:-

(not exists file “sometext.log” of folder (pathname of parent folder of regapp “besclient.exe”) whose (exists (line of it) whose (it contains “sucessful”)

thanks :slight_smile:

(imported comment written by jessewk)

You just need to move your whose clauses around a bit:

not exists file “sometext.log” whose (exists line whose (it contains “successful”) of it) of folder (pathname of parent folder of regapp “besclient.exe”)

(imported comment written by khanand91)

thanks very much

(imported comment written by RosaMartin)

what if you want to get all lines of a text file. Lines might vary in number.

(imported comment written by jessewk)

lines of file “c:\foo.txt”

(imported comment written by khanand91)

Is it possible to return ‘part of’ a line of text in a file ?

i.e.

if exists file (“C:\Program Files\Symantec\ESM\Bin\w3s-ix86\3sysver.log”) then following texts of firsts “3sysmod” of lines whose (it contains “3sysmod”) of file (“C:\Program Files\Symantec\ESM\Bin\w3s-ix86\3sysver.log”) else “n/a”

The output I get is as follows:-

27.0.0 (2006/06/27) - Installation (2006/06/27) - Installation

I’d just like to return the first 6 characters i.e. 27.0.0 … it’s probably easy but I can’t find and example on how to manipulate text

thanks

(imported comment written by sonny.mcmanigle91)

it might be something like:

preceding text of position 2 of (if exists file (“C:\test\num.txt”) then following texts of firsts “JUNK” of lines whose (it contains “JUNK”) of file (“C:\test\num.txt”) else “n/a”)

this would display JU out of JUNK

so just modify it for a 5 and encapsulate your relevance.

-Sonny

(imported comment written by BenKus)

Sonny’s relevance will work fine… but you can also rewrite it as:

first 6 of

which would be something like:

if exists file (“C:\Program Files\Symantec\ESM\Bin\w3s-ix86\3sysver.log”) then first 6 of following texts of firsts “3sysmod” of lines whose (it contains “3sysmod”) of file (“C:\Program Files\Symantec\ESM\Bin\w3s-ix86\3sysver.log”) else “n/a”

Ben

(imported comment written by khanand91)

Thank you, both worked well :slight_smile:

(imported comment written by SystemAdmin)

So… reviving this older thread: If one can, how would one the above results and select only the

last

line?

Here’s the statement we have so far:

lines whose (it contains “Update Finished”) of file “C:\Documents and Settings\All Users\Application Data\McAfee\DesktopProtection\UpdateLog.txt”

I want to get

just

the last line of these results so as to extract a date from that line to get the date of the most recent successful update. Is this possible?

(imported comment written by jessewk)

I can think of 2 ways right off the bat… chose the line with the highest line number, or parse out the time stamps and choose the maximum. The relevance for the time stamp version will be a little more straightforward. If you post an example line I can give you an exact relevance query, but in pseudo code it would look like this:

maximum of ({relevance to extract time stamp string} as time) of lines whose (it contains “Update Finsihed”)…

(imported comment written by BenKus)

FYI… Here is relevance to return the last line of a file:

q: line (number of lines of it) of file "C:\temp\test1.txt"
A: last line of this file

Ben

(imported comment written by PatNOregon)

Some good tips in this post but I need something just a bit different.

I need to search a file to check two things.

  1. that it has a specific string in it

  2. and whether or not that line with the string in it starts with a # or not.

I need to check whether or not this particular item is commented out and unfortunately since the entire line string is not consistant, I cant check for an exact match with the # in front, so I’m stuck trying to make this like a wildcard lookup.

Some thing like lines starting with “#” and contains “”

Any help?

(imported comment written by jessewk)

exists line whose (it starts with “#” and it contains “”) of file “c:\foo.txt”

(imported comment written by sonny.mcmanigle91)

Guys… i’m trying to recurse a text file that contains lines of absolute paths… I just want to run it in a loop and return the file sizes of them… Any input?

this code DOES NOT work:

(size of file (lines of file “c:\program files\bigfix enterprise\bes client\searchresults_pst.txt”)) as string & " KB"

I suck… help… too much vb recently, i’m forgetting my bigfix coding skills!

(imported comment written by jessewk)

(name of it, size of it) of files (lines of file “c:\program files\bigfix enterprise\bes client\searchresults_pst.txt”)