I have some scripts with code like:
AND EQUAL_NULL(b.DATE_FIELD, a.DATE_FIELD)
That I want to replace dynamically since b.DATE_FIELD
and a.DATE_FIELD
can have different fields.
I believe I need to capture each field within the parens as their own capture groups and write them to their new equivalents. Basically, I am thinking whatever is between (
and ,
would be group 1 and what is between ,
and )
would be group 2. then I can write them to the new substituted pattern shown below.
Desired End Result:
AND COALESCE(b.DATE_FIELD, 'NA') = COALESCE(a.DATE_FIELD, 'NA')
My attempt:
echo "AND EQUAL_NULL(b.DATE_LOCAL, a.DATE_LOCAL)" | sed -n "s/AND EQUAL_NULL(\([^,]*\)\([^)]*\).*/COALESCE(\1\, \'NA\'\) = COALESCE(\2\, \'NA\'\)/p"
returns most of what I want:
COALESCE(b.DATE_LOCAL, 'NA') = COALESCE(, a.DATE_LOCAL, 'NA')
I'm not quite sure how to get b.DATE_FIELD
as Group \1
and a.DATE_FIELD
AS Group \2
Updated Code which is closer to solution but still missing a piece
Tested on Linux with GNU Sed 4.9 using zsh
I tested this on MacOS zsh:
echo "AND EQUAL_NULL(b.DATE_LOCAL, a.DATE_LOCAL)" |
sed -n "s/AND EQUAL_NULL(\([^,]*\), \([^)]*\))/COALESCE(\1, 'NA') = COALESCE(\2, 'NA')/p"
Output:
COALESCE(b.DATE_LOCAL, 'NA') = COALESCE(a.DATE_LOCAL, 'NA')
I think you almost had the solution yourself.