0

我的脚本:

while read start_val
do
end_value = "$start_val+10" | bc

sed -n '"$start_value","$end_value"d'  <file>
done < in_file

实际上,我想通过一些计算使用从输入文件中获取的数据来打印文件的行。

4

4 回答 4

1

这是因为变量没有在单引号 (') 内展开。例如尝试

echo '$HOME'

对比

echo "$HOME"

您的示例也是如此,在 sed 之后使用单引号。

于 2013-04-18T09:40:58.400 回答
1

sed 是用于在单行上进行简单替换的出色工具,对于任何其他文本操作,只需使用 awk:

awk '
NR==FNR { in_file[FNR] = $0; next }
{ for (i=$0; i<=$0+10; i++) print in_file[i] }
' in_file file

并不是说您的原始脚本使用 -n 告诉 sed 不要打印任何行,然后 d 告诉 sed 删除一些行,这样它就不会产生任何输出,所以我不确定您真正想要做什么,awk脚本是一个猜测。

于 2013-04-18T12:40:32.207 回答
1

你可以像这样使用它:

sed -n "$start_value,$end_value"'d' $file

顺便说一句,不需要在那里使用 bc :

while read start_val
do
    end_value=$((start_val+10))
    sed "$start_value,$end_value"'d' $file
    # not sure what above sed is doing since you're not storing output anywhere!
done < in_file
于 2013-04-18T09:43:22.623 回答
0
sed -n $start_value','$end_value'p' file

这将打印从 $start_value 到 $end_value 的所有行。

于 2013-04-18T09:48:54.403 回答