linux - match and replace one pattern multiple times in the same line delimited by unknown characters -
given input of form:
select * foo ts>@(-2 days) , ts < @(-1 hour)
(this of course example. want support query construct , expression in @())
i want replace expression in every @()
result of evaluating date --date=exp +%s
, 'exp' expression (so in example above result
select * foo ts>1436694136 , ts < 1436863336
more generally, how replace pattern result of invoking shell command captures pattern arguments?
(i've tagged question perl, , bash, open anything)
using gnu sed:
$ sql_expr="select * foo ts>@(-2 days) , ts < @(-1 hour)" $ sed -r 's|@\(([^)]*)\)|$(date +%s --date "now\1")|g; s|.*|echo "&"|e' <<< "$sql_expr" gave: select * foo ts>1436696209 , ts < 1436865409
note code assumes contents of @(...)
valid expressions, date calculations.
explanation:
- first expression replace
@()
occurrences$(date +%s --date "now _expression_")
_expression_
going-2 days
. - second replace expression adds
echo "..."
around entire string & executes generated line. can removee
flag in second expression find out command gets executed actually.
Comments
Post a Comment