Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Calling SED using exec in TCL

1,333 views
Skip to first unread message

lac

unread,
May 23, 2006, 3:54:10 PM5/23/06
to
Hi,

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

Cameron Laird

unread,
May 23, 2006, 5:59:48 PM5/23/06
to
In article <1148414050.1...@i39g2000cwa.googlegroups.com>,

lac <luc.c...@gmail.com> wrote:
>Hi,
>
>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.
.
.
.
I don't understand your description. When I command

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]

SM Ryan

unread,
May 23, 2006, 10:42:00 PM5/23/06
to
"lac" <luc.c...@gmail.com> wrote:
# Hi,
#
# 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

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?

lac

unread,
May 24, 2006, 8:57:50 AM5/24/06
to
Thank you for your replies.

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

Arjen Markus

unread,
May 24, 2006, 9:08:15 AM5/24/06
to
Works perfectly for me ... Is the file that long?
What is the value of errorInfo?

Regards,

Arjen

neil mckay

unread,
May 24, 2006, 9:34:02 AM5/24/06
to
Is there any reason for not doing this in straight Tcl? The code's a
little longer than exec'ing sed, but it eliminates an external
dependency. Try this proc:

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

neil mckay

unread,
May 24, 2006, 1:16:28 PM5/24/06
to
Oops, I forgot one thing: if the picklines proc returns before getting
all the lines, it needs to close the file. Replace

if {$nChars < 0} {
return $linelist
}

with

if {$nChars < 0} {

lac

unread,
May 24, 2006, 4:27:29 PM5/24/06
to
Thank You Neil,

I tried using your solution and it seems to be as fast as using sed.

lac

0 new messages