Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

expect script - searching a text file

Status
Not open for further replies.

davidbelarus

Programmer
Oct 21, 2011
26
US
hello gentlemen.

i need to search a txt file, and once i find a particular string, extract something that starts with a word "root", and ends about 50 characters later with "grmem" (bootline for an stb)

text file has a term"fake boot" and shortly after it i anticipate to find the root and grmem



 
hi, this is how I would do it, since I am not so so with regular expressions:

Code:
set data "
bla1
root hahah, this one will be skipped grmem, so you know
bla3
bla fake boot hahaha
bla4
root hahha, place here a joke grmem and some more ...
bla5
root hahah, this one will be skipped grmem, so you know
bla6
bla7
ahum
"
set i [string first "root" $data [string first "fake boot" $data]]
set str [string range $data [expr $i+5] [expr [string first "grmem" $data [string first "root" $data $i]]-1]]

this returns:
hahha, place here a joke

variable "data" is the content of the alleged text file, question is, is it the appropriate format (without the laughs) ?
 
i put a little pseudocode to help me ID the components i need to gather.

[declare library] #!/usr/bin/expect -f
[input reading textfile name]
[loop through text file]
[string comparison algorithm] thacoda-above
[save found results in variable buildroot]
 
i put a little pseudocode to help me ID the components i need to gather.

[declare library] #!/usr/bin/expect -f
[input reading textfile name]
[loop through text file]
[string comparison algorithm] thacoda-above
[save found results in variable buildroot]
 
i am working on openning the file... so i can run the code thacoda wrote.

program runs, but did not figure out what it actually does.


----

#!/usr/bin/expect -f
#runline expect /home/david/Desktop/monday3.sh

send " we are going to open up a file for reading, ok? \n"
expect "ok"

set fileopen [open "/home/david/Desktop/text.txt" "r"]

set a [read $fileopen]
send "i am expecting to see a string from the file here $fileopen"

close $fileopen
 
with a little correction i was able to get this output:

6
entire text file written afterwards

it looks like "read" argument has the line number and set variable "a" has the entire file.

the files that i plan to read are like 20 pages. i wonder how safe it will be to run a program openning these big files.


#!/usr/bin/expect -f
#runline expect /home/david/Desktop/monday3.sh

send " we are going to open up a file for reading, ok? \n\n"
expect "ok"

set fileopen [open "/home/david/Desktop/text.txt" "r"]
set a [read $fileopen]

send "i am expecting to see a string from the file here $fileopen"
expect "cfe>"

send "another attempt to see what the file has $a"
expect "cfe>"

close $fileopen
 
Ok, then I would suggest the following way of working:

Code:
# from tG²
proc openFile {file} {
  set f [open $file]
  set content [read $f]
  close $f
  return $content
}

proc extractString {data} {
  set i [string first "root" $data [string first "fake boot" $data]]
  return [string range $data [expr $i+5] [expr [string first "grmem" $data [string first "root" $data $i]]-1]]
}

set data [openFile "/home/david/Desktop/text.txt"]
puts [extractString $data]
 
you could add at the end also:

set buildroot [extractString $data]

but, i guess that's obvious.
 
have you seen my code? nothing is obvious. :)

your sting works.
it does exactly what ineed it to, but i am not sure how it does it, and i need to understand it to use it again.

it looks like "set i [string... identifies the marker where needed information is, and where it begins"root. Can you help me understand how "set" command works in relation to isolating strings.

please explain what i+5 is?

I really appreciate this.
the fact that you gave me two functioning programs was very helpful.
thanks a lot.
david
 
additional puzzle i have facing me is: putting a string together from stuff i pulled out.

specific example:

i got this line i am pulling out: # buildroot_tag: buildroot_dev_dtvdev_v3b0044_20110815_dtvcm
i use buildroot: to signal start, and what i need to pull out is:

"buildroot_v3b0044_20110815"

it will be in a very similar file(text, with about 20 pages.
 
David,

Regarding your questions how it works, I propose that you examine the following pages first:


These are the basic tcl commands, this user manual is crucial while you learn tcl/tk. Also, check out "switch", it answers your search for a case statement. Try to come up with a solution for the additional puzzle you have based on the info in the link(s). Hint: look for "string" command, with option "range".

"please explain what i+5 is?"
'i' holds the character position within $data of the first "root " string AFTER string "fake boot". Since "root " has 5 characters, we need to extract the string that comes after i+5.

BTW, with time, you will see that there are maybe better, faster executing and/or simpler solutions...this is just one way of doing it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top