doulong6761 2017-08-01 14:42
浏览 149

php脚本上的sed bash命令

I need to delete some lines from my dhcpd.conf file by a simple php script. So I'm trying to use sed command to do that. On terminal I've tryed to use this command to delete from line 1 to 3 and it works:

sed -i '1,3d' pippo.txt

The problem is that i can't understand how to use sed passing some php variable for line numers. I need something like:

sed -i '$var1,$var2d' pippo.txt

can someone help me with the correct sintax for that command on php pls. Thanks.

  • 写回答

1条回答 默认 最新

  • douxiezha9319 2017-08-01 14:47
    关注

    Just use double quotes to expand the variables and wrap the variables with curly braces.

    var1=1
    var2=2
    sed -i "${var1},${var2}d" pippo.txt
    

    You can define a variable as $var2d. If you need to use the variable $var2, followed by letter d, then you must use curly braces as in ${var2}d. However this is not necessary for $var1 in this case as there is no ambiguity, but it's good to follow as a practice.

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大