Given the input of the form:
select * from foo where ts>@(-2 days) and ts < @(-1 hour)
(this is of course just an example. I want to support any query construct and any expression in @())
I want to replace the expression in every @()
with the result of evaluating date --date=exp +%s
, where 'exp' is the expression (so in the example above the result will be
select * from foo where ts>1436694136 and ts < 1436863336
More generally, how do I replace a pattern with the result of invoking a shell command with captures from the pattern as arguments?
(I've tagged the question with perl, and bash, but am open to anything)
Using gnu sed:
Note that this code assumes that the contents of
@(...)
are valid expressions, for date calculations.Explanation:
@()
occurrences with$(date +%s --date "now _expression_")
where_expression_
is going to be something like-2 days
.echo "..."
around entire string & executes the generated line. You can remove thee
flag in second expression to find out what command gets executed actually.Something like below (untested)