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:

  1. first expression replace @() occurrences $(date +%s --date "now _expression_") _expression_ going -2 days.
  2. second replace expression adds echo "..." around entire string & executes generated line. can remove e flag in second expression find out command gets executed actually.

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -