Home > OS >  Sed to how to realize the two commands, take effect when commands 1 to execute the command 2, otherw
Sed to how to realize the two commands, take effect when commands 1 to execute the command 2, otherw

Time:09-26

Sql_file. TXT file contents:
Select * from t1 as A where 1=1
Select * from t2 a where 1=1
Select * from t3
Select * from t4 A WHERE 1=1

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
I want to achieve: in a line, with the alias a after a where clause (case-insensitive, can have more Spaces), add a filter condition content as follows:
(A.U PD_DATE & gt; (SELECT LAST_CHK_DATE FROM table_xx WHERE sql_code='xx'))


-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
I in the script is written so:
="${sub_str} a where clause (A.U PD_DATE & gt; (SELECT LAST_CHK_DATE FROM table_xx WHERE sql_code='xx')) and ("

Sed -i "{s/[] [a, a] [] [] */w, w/h, h/e and e/r, r/e and e/${sub_str}/[]; S/$/)/} "${sql_file}


-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
My idea is that using the sed command match to a where clause, then a where clause to replace ${sub_str},
On the basis of the match to a where clause, plus a right parenthesis at the end of the line)

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
But now the problem is, the script will at the end of all lines are added to the right parenthesis. This is wrong, for t3 table should not add parentheses,

So, exactly how to implement? How to modify the sed command, make on the basis of the match to a where clause, to replace, line right parenthesis added again?

If this is the sed command: sed/pattern matching 1/replace 2/3
add parenthesesWhen 1 meet, executed 23; When 1 does not meet the 23 does not perform

CodePudding user response:

I contact the sed command also didn't take long, really it's killing me, I hope you can help me have a look at ~

CodePudding user response:

You to separate statements bai
Sed -i '/where/s/$/a)/' sql_file. TXT

CodePudding user response:

refer to the second floor zhouchao6 response:
you separate statements bai
Sed -i '/a/$/where/s)/' sql_file. TXT


Thank you, I really did this now, but a line of sed statements can achieve?