Sed has this:
sed -n -e '/from/,/where/ p' file.sql
Prints all the lines between a line with a from
and a line with a where
.
For something that can include lines that have both from and where:
#!/bin/sed -nf
/from.*where/ {
s/.*(from.*where).*/1/p
d
}
/from/ {
: next
N
/where/ {
s/^[^
]*(from.*where)[^
]*/1/p
d
}
$! b next
}
This (written as a sed script) is slightly more complex, and I'll try to explain the details.
The first line is executed on a line that contains both a from
and a where
. If a line matches that pattern, two commands are executed. We use the s
substitute command to extract only the parts between from and where (including the from and where). The p
suffix in that command prints the line. The delete command clears the pattern space (the working buffer), loading the next line and restarting the script.
The second command starts to execute a series of commands (grouped by the braces) when a line containing from
is found. Basically, the commands form a loop that will keep appending lines from the input into the pattern space until a line with a where
is found or until we reach the last line.
The :
"command" creates a label, a marker in the script that allows us to "jump" back when we want to. The N
command reads a line from the input, and appends it to the pattern space (separating the lines with a newline character).
When a where
is found, we can print out the contents of the pattern space, but first we have to clean it with the substitute command. It is analogous to the one used previously, but we now replace the leading and trailing .*
with [^
]*
, which tells sed to match only non-newline characters, effectively matching a from in the first line and a where in the last line. The d
command then clears the pattern space and restarts the script on the next line.
The b
command will jump to a label, in our case, the label next
. However, the $!
address says it should not be executed on the last line, allowing us to leave the loop. When leaving the loop this way, we haven't found a respective where
, so you may not want to print it.
Note however, this has some drawbacks. The following cases won't be handled as expected:
from ... where ... from
from ... from
where
from
where ... where
from
from
where
where
Handling these cases require more code.
Hope this helps =)