Change lines that match a pattern in between delimiters using awk

别来无恙 提交于 2019-12-06 02:07:22
/start_delimiter/ {inblock=1}
/end_delimiter/ {inblock=0;}
{ if (inblock==1 && (/more/)) { print "//" $0 } else print $0}

And for the one liners fanatics:

awk '/^(start_delim|end_delim)/{f=f?0:1}f&&/more/{$0="//" $0}1' file

Added the start of the line ^ anchor to avoid confusion if the block delimiter is embedded in the middle of a line.

you need something like

awk '/start_delimiter/,/end_delimiter/{
  if ($0 ~ /^more/ ) { print "//" $0 } 
  else print $0
}
!/start_delimiter/,/end_delimiter/ print $0' inputfile

The 2nd range pattern is to get print the other lines, outside of the range (specified by the leading '!') (I didn't have a way to test that right now, so try moving the '!' around if it doesn't work like this)

I hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!