replacing only last occurrence conditionally with awk / sed
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
(1) remove ( symbol from any line where ( symbol comes between ADD and CONSTRAINT clause 'ADD ( CONSTRAINT'
(2) Remove only last occurrence of ) symbol from any line containing word REFERENCES
for example this line
ADD ( CONSTRAINT myconst1 FOREIGN KEY(PID) REFERENCES MYTABLE );
I would warn against the issues of using '.*' when trying to catch specific characters, for example (however unlikely), your current example to remove a single '(' from between ADD and CONSTRAINT:
As you can see, if there was any pertinent data to be kept between ADD and second CONSTRAINT it has been lost.
As there is now columns to really worry about, I would probably used sed. You can use your same 2 substitutions (Daniel's work better) and use the -e option to submit multiple changes to the same row.
You could also do it all in one sed but it is pretty messy (maybe one of the sed gurus can tidy it up:
Code:
sed -r 's/((ADD)[^(]*\(([^C]*CONSTRAINT)|\)(.?)$)/\2\3\4/g'
(1) remove ( symbol from any line where ( symbol comes between ADD and CONSTRAINT clause 'ADD ( CONSTRAINT'
(2) Remove only last occurrence of ) symbol from any line containing word REFERENCES
I wonder if you want to change a line if and only if both conditions are true. If this is the case the solutions already posted may be inadequate.
>>I wonder if you want to change a line if and only if both conditions are true. If this is the case the solutions already posted may be inadequate.
Not really, it is possible that there is no REFERENCES clause in some lines but ADD ( CONSTRAINTS is still there
Also please note that there is no pertinent data or any string between ADD ( CONSTRAINTS
Thanks all, I will try your suggested answers and get back with results
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.