[英]How to get text between two patterns in awk?
Giving this input.txt: 提供此input.txt:
START asd
blah
blah
blah
START HELLO
lorem
ipsum
dolor
sit
amet
START STACK
bleh
bleh
I'm trying to get the lines between START HELLO
and START STACK
. 我正在尝试使START HELLO
和START STACK
之间的界线。
So this is the desired output : 所以这是所需的输出 :
START HELLO
lorem
ipsum
dolor
sit
amet
I did this awk: 我这样做awk:
awk '/START/{l++} {if(l==2){exit;} if(l==1) {print}}' input.txt
But returns the first START block, not the START HELLO
: 但是返回第一个START块,而不是START HELLO
:
START asd
blah
blah
blah
Do you have any idea to do it as clearer as possible? 您是否有想法将其变得更清晰? I've just started with awk few days ago, so any tip, help or advided will be appreciated. 我几天前刚开始使用awk,因此任何提示,帮助或建议都将不胜感激。
The blank lines are handy: you can use "paragraph" mode where each awk record is separated by blank lines instead of newlines: 空行很方便:您可以使用“段落”模式,其中每个awk记录都由空行而不是换行符分隔:
awk -v RS="" '/^START HELLO/' file
If the "hello" is to be passed in as a parameter: 如果将“ hello”作为参数传递:
awk -v RS="" -v start=HELLO '$1 == "START" && $2 == start' file
IF you need to specify between START HELLO
and START STACK
regardless of space paragraph: 如果您需要在START HELLO
和START STACK
之间指定,而不考虑空格段:
awk '/START HELLO/ {f=1} /START STACK/ {f=0} f;' file
START HELLO
lorem
ipsum
dolor
sit
amet
It will be a more exact answer to the question: (and better if you need multiple sections) 这将是对该问题的更精确答案:(如果需要多个部分,则更好)
I'm trying to get the lines between START HELLO and START STACK.
I would normal go for solution from Glenn, but its not true to the question 我通常会向格伦寻求解决方案,但对这个问题并不正确
awk -v RS="" '/^START HELLO/' file
Your indexing is off. 您的索引已关闭。 Simply change your awk to: 只需将您的awk更改为:
awk '/START/{l++} {if(l==3){exit;} if(l==2) {print}}' input.txt
To print the empty-line-separated block that starts with "START HELLO": 要打印以“ START HELLO”开头的以空行分隔的块:
awk -v RS= '/^START HELLO/' file
To print the text between "START HELLO" and the next line that starts with "START": 要在“ START HELLO”和以“ START”开头的下一行之间打印文本:
awk '/^START HELLO{f=1} f{if (/^START/) exit; else print}' file
To print the text between "START HELLO" and the next line that starts with "START STACK": 要在“ START HELLO”和以“ START STACK”开头的下一行之间打印文本:
awk '/^START HELLO{f=1} f{if (/^START STACK/) exit; else print}' file
If you are every considering a solution that uses getline
, it is probably the wrong approach so make sure you read http://awk.info/?tip/getline and fully understand the appropriate uses and all of the caveats before making a decision. 如果您正在考虑使用getline
的解决方案,那可能是错误的方法,因此在做出决定之前,请确保您已阅读http://awk.info/?tip/getline并充分理解了适当的用法和所有注意事项。
我认为这可能会解决您的问题:
awk '/START HELLO/{print;while(getline)if($0 !~/START STACK/)print;else exit}' input.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.