如何使用tcl文件中的sed [英] how to use sed from a tcl file

查看:542
本文介绍了如何使用tcl文件中的sed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在tcl文件中使用Unix"sed"命令格式,如下所示: (将多个空格更改为一个空格)

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

我还尝试了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: `''

从Linux终端运行时,该命令运行正常

The command works fine when run from a linux terminal

我在做什么错了?

推荐答案

我在做什么错了?

What am I doing wrong?

您做错了什么是使用'(单引号)字符.它们对Tcl 完全没有特殊意义. Tcl中的等效项是将一个单词括在{大括号}中;它完全不对里面的字符进行任何特殊处理.因此,您想要做的是:

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

请介意,如果您要做的事情更复杂,并且Tcl限制了整个单词不被引用,那么您可以选择这样做:

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"

或者, real 惯用的Tcl只是不将sed用于以下简单的操作:

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

这篇关于如何使用tcl文件中的sed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆