user3299633 user3299633 - 1 month ago 7
Linux Question

Need to capture number of lines between two search criteria

I need to capture the number of lines between each DELETE statement.

DELETE FROM database.table
WHERE
@1=A
@2=B
@3=C
@4=D
@5=D
DELETE FROM database.table
WHERE
@1=A
@2=B
@3=C
@4=D
@5=D
DELETE FROM database.table
WHERE
@1=A
@2=B
@3=C
@4=D
@5=D


So I would get a returned status of "5"

I've tried the following:

awk '/DELETE/{exit}flag;/WHERE/{flag=1}' delete_statements | wc -l


But this only works if I delete the first line in the file.

Answer

To skip the first line from within awk, just add the condition:

awk 'NR>1 && /DELETE/{exit}flag;/WHERE/{flag=1}'
     ~~~~
Comments