打印从正则表达式到文件末尾的所有行

时间:2013-09-03 11:28:52

标签: bash sed

长时间潜伏,第一次海报:) 我必须编写一个代码,其中有一部分我需要从正则表达式到文件结尾的文件中的所有行

我的代码:

if [ -z "$start" ]
then
  if [ -z "$stop" ]
  then
    echo "all functions"
  else
     echo "from beginning till stop function"
    sed -n "/$stop/I,\$!p" timings.txt > newtimings.txt
  fi
else
  if [ -z "$stop" ]
  then
    echo "start function to end "
    sed -n "/$start/I,\$p" timings.txt > newtimings.txt

  else
    echo "start func to stop func"
    sed -n "/$start/I,/$stop/Ip" timings.txt > newtimings.txt
  fi
fi

我的代码行,我假设有一个值为start但NULL为stop,即第二个sed语句,应该从START regex打印到文件结尾似乎不起作用。 已经通过这里的许多帖子仍然无法让它工作

1 个答案:

答案 0 :(得分:2)

问题是,由于你的sed表达式用双引号括起来,shell会扩展其中的所有变量。因此,第二个$p命令中的sed会扩展为空字符串,因此sed看到/startPattern/,无效。

尝试像这样逃避美元:

sed -n "/$start/,\$p" timings.txt > newtimings.txt

或者,在$p周围使用单引号,以便shell不会展开它:

sed -n "/$start/,"'$p' timings.txt > newtimings.txt
相关问题