Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to use the Unix "sed" command form within a tcl file, like this: (to change multiple spaces to one space)

exec /bin/sed 's/ +/ /g' $file

I also tried exec /bin/sed 's/ \+/ /g' $file (an extra backslash)

none of the version work, and I get the error

/bin/sed: -e expression #1, char 1: Unknown command: `''

The command works fine when run from a linux terminal

What am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
754 views
Welcome To Ask or Share your Answers For Others

1 Answer

What am I doing wrong?

What you're doing wrong is using ' (single quote) characters. They're not special to Tcl at all. The equivalent in Tcl is enclosing a word in {braces}; it gives no special treatment at all to the characters inside. Thus, what you seek to do would be:

exec /bin/sed {s/ +/ /g} $file

Mind you, if you're doing something more complex and the restriction of Tcl to whole-words being unquoted, then you might instead go for this:

exec /bin/sh -c "sed 's/ +/ /g' $file"

Or, real idiomatic Tcl just doesn't use sed for something this simple:

set f [open $file]
set replacedContents [regsub -all { +} [read $f] " "]
close $f

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...