LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-07-2017, 08:50 AM   #1
manish26
LQ Newbie
 
Registered: Feb 2017
Posts: 3

Rep: Reputation: Disabled
replacing only last occurrence conditionally with awk / sed


hello experts

I have got a below situation in a Unix file..

cat file1

CREATE table ( col1 int) ;
ADD ( CONSTRAINT myconst1 FOREIGN KEY(PID) REFERENCES MYTABLE );
ADD ( CONSTRAINT myconst1 FOREIGN KEY(PID) REFERENCES MYTABLE )
;

What I want to do now is this

(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 );

should be converted to

ADD CONSTRAINT myconst1 FOREIGN KEY(PID) REFERENCES MYTABLE ;

appreciate your help
 
Old 02-07-2017, 09:30 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Sounds like a plan ... what have you done to attempt to solve this and where are you stuck?
 
Old 02-07-2017, 09:34 AM   #3
manish26
LQ Newbie
 
Registered: Feb 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
this is what I tried

awk '{sub(/ADD.*CONSTRAINT/, "ADD " "CONSTRAINT"); print} | awk '/REFERENCES/ {sub(/\)/,"")}1'

seems like first part is working but second part of awk is not giving me desired output
 
Old 02-07-2017, 10:05 AM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by manish26 View Post
this is what I tried

awk '{sub(/ADD.*CONSTRAINT/, "ADD " "CONSTRAINT"); print} | awk '/REFERENCES/ {sub(/\)/,"")}1'
Your solution is almost right. Try this variation:
Code:
awk '{sub(/ADD.*CONSTRAINT/, "ADD CONSTRAINT")
    /REFERENCES/ sub(/\);$/,";")}1' $InFile >$OutFile
Note that only one awk is needed.

Daniel B. Martin
 
Old 02-07-2017, 10:35 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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:
Code:
ADD ( CONSTRAINT myconst1 FOREIGN KEY(PID) REFERENCES MYTABLE CONSTRAINT myconst1 FOREIGN KEY(PID) REFERENCES MYTABLE );
# output of your awk
ADD CONSTRAINT myconst1 FOREIGN KEY(PID) REFERENCES MYTABLE );
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 members found this post helpful.
Old 02-07-2017, 10:47 AM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by manish26 View Post
What I want to do now is this

(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.

Daniel B. Martin
 
Old 02-07-2017, 11:08 AM   #7
manish26
LQ Newbie
 
Registered: Feb 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
>>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
 
Old 02-08-2017, 03:15 PM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,806

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
The following might be better readable
Code:
sed '
s/\(ADD\) *[(] *\(CONSTRAINT\)/\1 \2/
s/\(REFERENCES.*\)[)]/\1/
' file1
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Replace 2nd occurrence of a string in a file - sed or awk? kushalkoolwal Programming 26 09-26-2021 04:10 PM
[SOLVED] Need help in replacing set of characters in a specific line using sed or awk bbachu Programming 15 01-03-2011 01:01 AM
[SOLVED] SED and Replacing Specific occurrence or Range of Lines bridrod Linux - Newbie 7 08-27-2009 09:59 AM
Replacing text on specific lines with sed or awk? Lantzvillian Linux - Newbie 5 10-17-2007 09:00 AM
Replacing a place holder on a template with bash, sed, and or awk rignes Programming 7 02-16-2006 04:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration