Reputation: 1414
I can't seem to find a good way to do this using Unix/Linux commands (without writing a script). I'm thinking this can be done, though.
What I want to do is read the contents of a file, split them by some delimiter, and then discard any segments that don't match a RegEx (essentially, a map-filter-reduce problem).
something like
cat original-file.sql | split --separator=';' | pcregrep -M '(.|\n)*needed_schema(.|\n)*' | result-file.sql
so that the result file only contains SQL statements that include 'needed_schema' in the statement.
original-file.sql
SELECT *
FROM information_schema.tables
WHERE table_schema = 'public'
;
SELECT *
FROM needed_schema.some_table
WHERE some_col = 'some value'
;
SELECT *
FROM needed_schema.some_other_table
WHERE some_other_col = 'some other value'
;
result-file.sql
SELECT *
FROM needed_schema.some_table
WHERE some_col = 'some value'
;
SELECT *
FROM needed_schema.some_other_table
WHERE some_other_col = 'some other value'
;
Upvotes: 1
Views: 175
Reputation: 47099
Consider using awk and assign RS
(Record Separator) and ORS
(Output Record
Separator):
$ awk '/needed_schema/' RS=';' ORS=';' input
SELECT *
FROM needed_schema.some_table
WHERE some_col = 'some value'
;
SELECT *
FROM needed_schema.some_other_table
WHERE some_other_col = 'some other value'
;
The regex is not PCRE but Extended Regular Expression
Upvotes: 2