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

查看:20
本文介绍了如何使用 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

我做错了什么?

推荐答案

我做错了什么?

您做错的是使用 '(单引号)字符.它们对于 Tcl 根本不是.Tcl 中的等价物是在 {braces} 中包含一个单词;它根本没有对里面的字符进行特殊处理.因此,您想要做的是:

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"

或者,真实惯用的 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天全站免登陆