在线编辑扩展shell变量

时间:2014-09-09 09:04:20

标签: shell variables expansion ed

我想在具有非常有限的shell功能的unix系统上缩短日志文件。我首选的方法是使用ed

删除固定数量的行可以正常工作:

ed -s file.txt <<< $'1,4d\nwq'

但是如何从ed oneliner中的shell变量扩展要删除的行数?我正在寻找类似的东西:

n_del=4; ed -s file.txt <<< $'1,\${n_del}d\nwq'

1 个答案:

答案 0 :(得分:3)

虽然在shell中只是连接以不同方式引用的字符串甚至是非引用的字符串是完全正常的,所以它可能看起来像:

$ n_del=4; ed -s test <<< "1,${n_del}"d$'\n'wq

我相信,here-doc在这里会比单行更清洁:

$ n_del=4
$ ed -s test <<_EOF
1,${n_del}d
wq
_EOF
相关问题