本文介绍了在符合使用AWK分隔符之间的图案线条的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入文件是这样的:

 一些行
其他一些线路
另一条线
start_delimiter
  有趣的东西
  更有趣的东西
  更有趣的东西
end_delimiter
可能更多的东西

我想操纵匹配的正则表达式模式的start_delimiter和end_delimiter之间的界限,并将结果写入到输入文件。例如,添加//到包含字行的开头更多(只要这些行分隔符之间):

 一些行
其他一些线路
另一条线
start_delimiter
  有趣的东西
  //更多有趣的东西
  //更有趣的东西
end_delimiter
可能更多的东西

我可以得到文字这样的分隔符之间的部分:

 的awk'/ start_delimiter /,/ end_delimiter /'inputfile中

如果我管这另一个AWK我可以改变行我感兴趣的:

 的awk'/多/ {子(/ ^ /,//)} 1'

我不知道的是如何回去替换为新内容的分隔部分是什么。有任何想法吗?积分为一个班轮。


解决方案

  / start_delimiter / {inblock = 1}
/ end_delimiter / {inblock = 0;}
{如果(inblock == 1安培;及(/以上/)){打印//$ 0}打印别人$ 0}

I have an input file something like this:

some line
some other line
another line
start_delimiter
  interesting stuff
  more interesting stuff
  even more interesting stuff
end_delimiter
possibly more stuff

I want to manipulate the lines between start_delimiter and end_delimiter that match a regex pattern and write the results to the input file. For example, add '//' to the beginning of the lines containing the word 'more' (as long as those lines are between the delimiters):

some line
some other line
another line
start_delimiter
  interesting stuff
  //more interesting stuff
  //even more interesting stuff
end_delimiter
possibly more stuff

I can get the section of text between the delimiters this way:

awk '/start_delimiter/,/end_delimiter/' inputfile

If I pipe this to another awk I can change the lines I'm interested in:

awk '/more/ {sub(/^/,"//")}1'

What I'm not sure about is how to go back and replace the delimited section with the new contents. Any ideas? Bonus points for a one-liner.

解决方案
/start_delimiter/ {inblock=1}
/end_delimiter/ {inblock=0;}
{ if (inblock==1 && (/more/)) { print "//" $0 } else print $0}

这篇关于在符合使用AWK分隔符之间的图案线条的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 16:33