LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-30-2011, 01:00 PM   #1
linuxred
LQ Newbie
 
Registered: May 2011
Posts: 9

Rep: Reputation: Disabled
shell script for below logic


Hi Team,

I need shell script for below logic. I have file

string1
string2
string3
ALTER TABLE TNAME
ADD COLUMN (C1 DATATYPE);
String4
alter table tname1
add column
(c1 number,
c2 char)
;
string 5

Now i need to extract ALTER statememnts...read from ALTER statement and end till ; is encountered.

ALTER statements are not fixed in length.

I need shell script to read the above file and extract alter commands till ;

please help
 
Old 05-30-2011, 01:05 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Code:
awk '/^ALTER/,/^;$/' file
Welcome to LinuxQuestions!
 
Old 05-30-2011, 01:08 PM   #3
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,093

Rep: Reputation: 197Reputation: 197
This smells a lot like homework. How about you post what you have so far, and then maybe someone can tell you where you're going wrong.
 
1 members found this post helpful.
Old 05-30-2011, 01:24 PM   #4
linuxred
LQ Newbie
 
Registered: May 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
Code:
awk '/^ALTER/,/^;$/' file
Welcome to LinuxQuestions!
Thanks, but that does not seem to fit my purpose. The output of above command is

ALTER TABLE ADD COLUMN
(COL1)
;
STRING3
ALTER TABLE....
STRING 4

I am getting in between strings also. Only it has removed the strings above first ALTER. I want only ALTER commands no strings in between
 
0 members found this post helpful.
Old 05-30-2011, 01:32 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Code:
awk '/^ALTER/{_=1}/^;$/{print;_=0}_'
Question: is there always a semi-colon (on its own line) to close the group of lines after ALTER? Or is there an ALTER toward the end of the file that is not closed by a semi-colon? Another question: can you understand the above code and try to elaborate it in order to match your requirements? In other words: what are your awk skills?
 
Old 05-30-2011, 01:41 PM   #6
linuxred
LQ Newbie
 
Registered: May 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
Code:
awk '/^ALTER/{_=1}/^;$/{print;_=0}_'
Question: is there always a semi-colon (on its own line) to close the group of lines after ALTER? Or is there an ALTER toward the end of the file that is not closed by a semi-colon? Another question: can you understand the above code and try to elaborate it in order to match your requirements? In other words: what are your awk skills?

ALTER statement is always ended by ; ...I need to extract only ALTER statement.

I am not confortable with awk commands. The output did not change...

I am getting

ALTER TABLE......
....
;
string 3;
ALTER TABLE .....
...
;

Last edited by linuxred; 05-30-2011 at 01:47 PM.
 
0 members found this post helpful.
Old 05-30-2011, 02:13 PM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Ok. Maybe it depends on how the ending semi-colon is placed. My code looks for a semi-colon without any other character on the same line, that is no spaces before and after. Try to change the regular expression to something more suitable to your input:
Code:
/^;$/
for example you may try something like:
Code:
/^;/  # semi-colon at the beginning of the line followed by anything
/;$/  # semi-colon at the end of the line
/;/   # semi-colon in the middle of other stuff (if any)
awk code explained:
Code:
/^ALTER/ {
  #
  #  We encounter the ALTER statement at the beginning of the line:
  #  set the flag _ to 1 (TRUE)
  #
  _ = 1
}

/^;$/ {
  #
  #   We encounter a semi-colon alone in its own line:
  #   print the semicolon and set the flag _ to 0 (FALSE)
  #
  print
  _ = 0
}

#
#  Depending on the value of the _ flag the line is printed or not
#
_
 
Old 05-30-2011, 02:23 PM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
colucix: It's obvious from the original post that the semicolon is not on the same line. This should work:

Code:
awk 'BEGIN { RS="" } { gsub(/ALTER[^;]*;/, "", $0); print }'
 
Old 05-30-2011, 02:53 PM   #9
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 19,321

Rep: Reputation: 4470Reputation: 4470Reputation: 4470Reputation: 4470Reputation: 4470Reputation: 4470Reputation: 4470Reputation: 4470Reputation: 4470Reputation: 4470Reputation: 4470
Quote:
Originally Posted by linuxred View Post
ALTER statement is always ended by ; ...I need to extract only ALTER statement.
I am not confortable with awk commands. The output did not change...I am getting

ALTER TABLE......
....
;
string 3;
ALTER TABLE .....
...
;
I agree with arizonagroovejet...why don't you post what you've written? Or are you wanting us to write and debug your scripts for you?
 
Old 05-30-2011, 08:54 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,564

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
Might also be nice if you posted the expected output seeing as the guessing game to get what you want is not working.
 
Old 05-31-2011, 12:22 AM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
Seems to me those are (prob Oracle) SQL Alter Table ...; statements, with other strings in between (possibly comments).
He just wants the SQL out, presumably to run directly. Therefore, give the example in post #1, output would be
Code:
ALTER TABLE TNAME
ADD COLUMN (C1 DATATYPE);

alter table tname1
add column
(c1 number,
c2 char)
;
 
Old 05-31-2011, 12:46 AM   #12
linuxred
LQ Newbie
 
Registered: May 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
Hi,

I will work with your inputs. Thanks for your time and information
 
0 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] A shell script to search and replace strings on certain logic rockie321 Linux - Newbie 5 06-16-2011 05:04 PM
nested loop-bash script- issue on logic yathin Linux - Newbie 6 05-31-2010 07:30 AM
LSI Logic / Symbios Logic 53c875 (rev 14) -> HP Storageworks 1/8 G2 gileravxr Linux - Hardware 0 07-21-2009 05:45 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:32 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration