DEV Community

[Comment from a deleted post]
Collapse
 
drhyde profile image
David Cantrell • Edited

perl -pne 's/^(SQL: |(?<!SQL: ).*)//s'

-e means execute this ...
-n ... for each line in the input ...
-p ... and print the result

s/...// replaces anything that matches with the empty string. The /s on the end makes . match newlines.

^ matches the beginning of a line

^(foo|bar) matches either foo or bar at the beginning of the line

(?<!foo)bar is a negative lookbehind assertion, it matches bar if not preceded by foo, so (?<!SQL: ).* matches any text not preceded by SQL:

So the whole regex means "match either SQL: at the beginning of a line, or any line not beginning with SQL: and replace it with the empty string". All we're left with is the SQL queries.

And if I've counted correctly, the bit inside 'single quotes' is one character shorter than the two vim commands and the newline between them, although I'll not mind if you consider the -pne to be cheating :-)

Collapse
 
trueneu profile image
Pavel Gurkov

Yay, and here it is: the shorter way. David, have you ever played regex golf? :)