How to use sed from tcl file

I am trying to use the Unix "sed" command form in a tcl file, for example: (to change multiple spaces to one space)

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

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

none of the versions work and I get an error

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

The command works great when launched from linux terminal

What am I doing wrong?

+7
unix sed tcl exec
source share
2 answers

What am I doing wrong?

What are you wrong with, use the characters ' (single quotes). They are not at all special to Tcl. Equivalent in Tcl encloses a word in { brackets } ; he does not give any special treatment to the characters inside. So you want to do the following:

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

Keep in mind if you are doing something more complex and restricting Tcl to whole words that are not ordered, then you can go for this instead:

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

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

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

Use exec /bin/sed "s/\ +/\ /g" $file

"\" tells TCL that there is a place. Also, using `` '', the string is correctly configured.

+1
source share

All Articles