I have been trying to call SED from TCL using the exec function and it
behaves differently then if I called the same SED function from the
shell (command line).
I am trying to extract only certain lines from a file as follow:
sed -n '5,10p' $Filename
If I type this from the command line in ksh, I get what I expect, lines
5 to 10 are printed to standard output.
However, if I try to call that same function within TCL using exec,
nothing returns.
catch {exec ksh -c "sed '5,10p' $Filename"} content
I have tried not using -n on sed but then all the file gets printed to
standard output when called from command line with duplicates of lines
5 to 10. When I don't use the -n flag from withing TCL (with exec), all
the file is printed without duplicates of line 5 to 10.
Can someone help me with this? I don't understand what is happening
here?
Thank you,
lac
catch {exec ksh -c "sed -n '5,10p' $Filename"} content
I see the five lines I expect in $content.
There are several other possibilities. You might like
set content [exec sed -n 5,10p $Filename]
You shouldn't need to use ksh; and if $Filename contains shell
special characters, it will not work. You should be able to do
catch {exec sed {5,10p} $Filename} content
You should only call a shell -c through exec if there some kind
of shell processing of the command string you need that you cannot
(conveniently) do in Tcl.
--
SM Ryan http://www.rawbw.com/~wyrmwif/
I hope it feels so good to be right. There's nothing more
exhilarating pointing out the shortcomings of others, is there?
I found that when I use the following line, it behaves like I want it
too:
set content [exec sed -n 5,10p tools.wish]
where tools.wish is my filename.
Now if I want to set the start and end lines dynamically as follows, it
doesn't work (or returns an empty string)
set start 5
set end 10
set content [exec sed -n ${start},${end}p tools.wish]
I am lost! Thanks for helping,
lac
Regards,
Arjen
proc picklines {fname startline endline} {
if {[catch [list open $fname "r"] fd] != 0} {
return [list]
}
set linelist [list]
set lineNo 0
while {$lineNo < $endline} {
set nChars [gets $fd line]
if {$nChars < 0} {
return $linelist
}
incr lineNo
if {$lineNo >= $startline} {
lappend linelist $line
}
}
close $fd
return $linelist
}
Neil
if {$nChars < 0} {
return $linelist
}
with
if {$nChars < 0} {
I tried using your solution and it seems to be as fast as using sed.
lac