简体   繁体   English

awk在一个文件中两个单词之间的所有行出现多次

[英]awk all lines between two words in one file with multiple occurrences

I am trying to get all lines in some SQL code between WHERE and GROUP, I have the below, which gets me the first occurrence of text between WHERE and GROUP, but there are multiple occurrences of the same I am after 我正在尝试在WHERE和GROUP之间的某些SQL代码中获取所有行,下面是其中的代码,它使我在WHERE和GROUP之间的文本第一次出现,但是我多次出现在同一位置

awk '/WHERE/{p=1} p; /GROUP/{exit}' filename.txt

Output 产量

WHERE something Some SQL code GROUP BY something

There are multiple sections of the code that start with WHERE and end with GROUP BY with in the file I would like to output 我要输出的文件中有多个代码部分以WHERE开头并以GROUP BY结尾

Can anybody help? 有人可以帮忙吗?

It is better in awk to do something along these lines: 这是更好的awk做沿着这些路线的东西:

awk '/WHERE/{f=1} f; /GROUP/{f=0}' file

The awk range operator , works similarly to sed . awk范围操作,工程同样sed However, it is difficult to modify and you limit what awk can do. 但是,很难修改,并且限制了awk可以执行的操作。

Once your awk habit includes using a flag (rather than a range) it will be easier to print between marks such as: 一旦您的awk习惯包括使用标记(而不是范围),则将更容易在标记之间进行打印,例如:

$ echo "a
b
c
---
d
e
f
---
g
h" | awk '/^---$/{f= ! f; next}  f'
d
e
f

Which is impossible with the range operator. 使用范围运算符是不可能的。

awk '/WHERE/,/GROUP/' filename.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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