0

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

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

Can anybody help?

Anthony
  • 95
  • 2
  • 11
  • 1
    try resetting `p` to `0` where you are past the `GROUP` condition. – shellter Aug 17 '17 at 21:44
  • I'm curious - what do you think your posed code means? If you know what it means then it's utterly trivial to change to do what you want. – Ed Morton Aug 17 '17 at 21:59
  • 1
    Possible duplicate of [awk range and selection of text](https://stackoverflow.com/questions/43982874/awk-range-and-selection-of-text) – dawg Aug 17 '17 at 22:05

2 Answers2

2

It is better in awk to do something along these lines:

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

The awk range operator , works similarly to sed. However, it is difficult to modify and you limit what awk can do.

Once your awk habit includes using a flag (rather than a range) it will be easier to print between marks such as:

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

Which is impossible with the range operator.

dawg
  • 98,345
  • 23
  • 131
  • 206
-1
awk '/WHERE/,/GROUP/' filename.txt
Anthony
  • 95
  • 2
  • 11
  • Usually it's better to explain a solution instead of just posting some rows of anonymous code. You can read [How do I write a good answer](https://stackoverflow.com/help/how-to-answer), and also [Explaining entirely code-based answers](https://meta.stackexchange.com/questions/114762/explaining-entirely-%E2%80%8C%E2%80%8Bcode-based-answers). – Massimiliano Kraus Aug 18 '17 at 08:52