首页 > 解决方案 > 在 tcl 文件中使用 sed

问题描述

嗨,我是新的 tcl,遇到了如下问题。

在这段代码中,我试图从文件中只获取第 1 行和第 2 行,并希望将它们放入另一个变量中。但是这段代码对我不起作用。

set sed_com "${line1},${line2}p";               # the value of this sed command is 1,2p
set tmp  [exec sh -c {sed -n "${sed_com}" file.tmp | tail -n 1}]; # this one won't work 
set tmp  [exec sh -c {sed -n "1,2p" file.tmp | tail -n 1}];       # this one will work

我想知道是否有一种方法可以让 tcl 文件像第 3 行一样解释它?

标签: sedtcl

解决方案


你为什么把事情复杂化sh -c?你可以这样做:

set tmp [exec sed -n $sed_com file.tmp | tail -n 1]

推荐阅读