Reputation: 125
I am trying to understand how to work with expect.
I want to check if a file has a certain string in it and if it does contain it than delete the whole line, i know how i would do that with bash with if and grep, but im fairly new to expect and i am having issues with getting it to work the basic idea is this ( in bash script )
if grep -q "string" "file";
then
echo "something is getting deleted".
sed -i "something"/d "file"
echo Starting SCP protocol
else
echo "something was not found"
echo Starting SCP protocol.
fi
Thanks in advance.
Upvotes: 0
Views: 4593
Reputation: 247210
Since @whjm deleted his answer, here's a pure-Tcl way to accomplish your task:
set filename "file"
set found false
set fh [open $filename r]
while {[gets $fh line] != -1} {
if {[regexp {string} $line]} {
puts "something is getting deleted"
close $fh
set fh_in [open $filename r]
set fh_out [file tempfile tmpname]
while {[gets $fh_in line] != -1} {
if {![regexp {something} $line]} {
puts $fh_out $line
}
}
close $fh_in
close $fh_out
file rename -force $tmpname $filename
set found true
break
}
}
if {!$found} {close $fh}
puts "Starting SCP protocol"
Upvotes: 1
Reputation: 247210
Another approach: using Tcl as a glue language, like the shell, calling out to system tools:
if {[catch {exec grep -q "string" "file"} output] == 0} {
puts "something is getting deleted".
exec sed -i "something"/d "file"
} else {
puts "something was not found"
}
puts "Starting SCP protocol"
See https://wiki.tcl.tk/1039#pagetoce3a5e27b for a thorough explanation of using catch
and exec
More current Tcl would look like
try {
exec grep -q "string" "file"
puts "something is getting deleted".
exec sed -i "something"/d "file"
} on error {} {
puts "something was not found"
}
puts "Starting SCP protocol"
Upvotes: 1