简体   繁体   中英

print all lines from regex to end of file

Long time lurker, first time poster :) I have to write a code of which there is a part where I need all the lines from a file starting from a regex till end of file

My code:

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

The line of my code where i assume there is a value for start but NULL for stop,ie, the 2nd sed statement,which should print from START regex till end of file doesn't seem to work. Have been through many posts here still could not get it to work

The problem is that, since your sed expression is enclosed within double quotes, the shell expands all variables inside it. As a result, $p in your second sed command is expanded into an empty string, so sed sees /startPattern/, which is not valid.

Try escaping the dollar like this:

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

Alternatively, use single quotes around $p so that the shell does not expand it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM