Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate and modify lines using Sed

Tags:

sed

Let's consider a SQL file as input (i.e. myTable.sql) containing the following statement :

EXECUTE IMMEDIATE 'CREATE OR REPLACE PUBLIC SYNONYM ' || myTable || ' FOR SCHEMA1.' || myTable;

Is there a simple way (using sed) to duplicate this statement and modify them as follows ?

EXECUTE IMMEDIATE 'CREATE OR REPLACE SYNONYM SCHEMA2.' || myTable || ' FOR SCHEMA1.' || myTable;
EXECUTE IMMEDIATE 'CREATE OR REPLACE SYNONYM SCHEMA3.' || myTable || ' FOR SCHEMA1.' || myTable;

Thanks a lot for your help. M.

* EDIT *

First part can be solved like this :

sed -i -e '/PUBLIC SYNONYM/p' myTable.sql

Second part is about two search-and-replace commands :

sed -i -e '0,/PUBLIC SYNONYM/s/PUBLIC SYNONYM /SYNONYM SCHEMA2./' myTable.sql
sed -i -e '0,/PUBLIC SYNONYM/s/PUBLIC SYNONYM /SYNONYM SCHEMA3./' myTable.sql

I consider my question has been answered. Feel free to reply if you find something more efficient. Cheers.

like image 432
Mildju Avatar asked Apr 13 '26 22:04

Mildju


1 Answers

First part can be solved like this :

sed -i -e '/PUBLIC SYNONYM/p' myTable.sql

Second part is about two search-and-replace commands :

sed -i -e '0,/PUBLIC SYNONYM/s/PUBLIC SYNONYM /SYNONYM SCHEMA2./' myTable.sql
sed -i -e '0,/PUBLIC SYNONYM/s/PUBLIC SYNONYM /SYNONYM SCHEMA3./' myTable.sql

I consider my question has been answered. Feel free to reply if you find something more efficient. Cheers.

like image 116
Mildju Avatar answered Apr 17 '26 08:04

Mildju



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!