Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

; followed by nothing else but ;

Status
Not open for further replies.

IMAUser

Technical User
May 28, 2003
121
CH

Hi
I have a file which has things like
CREATE TABLE ....
...
;
;

CREATE TABLE ...
;

CREATE TABLE ...
; ;

CREATE TABLE ...
/

CREATE TABLE ...
/
;

I need to find out the occurances of semi colon followed by nothing then a semicolon.

Is there a way to do it in some kind of a awk script...

Many Thanks,
 
Sorry,

I would expect to see
---------------------------------
1 CREATE TABLE ....
2 ....
3 ;
4 ;
5
6 CREATE TABLE ...
7 ;
8
9 CREATE TABLE ...
10 ; ;
11
12 CREATE TABLE ...
13 /
14
15 CREATE TABLE ...
16 /
17 ;
---------------------------

Sorry, I didnt know how to mark the lines so I have added a line number. SO I would expect to see line no 3-4, 10, 16-17

Actually, Ideally I want to write a script which looks for the keyword CREATE and then looks for a semicolon in the same or the following lines. After the semicolon it shld again look for CREATE, but if it finds a semicolon it should flag an error.

But ofcourse at the very end of the script there will only be a semicolon and it will be followied by nothing.

Simply put any CREATE should only have one semicolon, whereas atm there are more then one.

Hope I have explained it well.

Thanks for your help.
 
Hi

Hmm... I would suggest abit different approach. Set the [tt]RS[/tt] ( Record Separator ) to semi-colon ( ; ).
Code:
awk -vRS=';' '{print"--=[ Record "NR" ]=-- -- -- -- -- --\n"$0}' /input/file
In which case your output would look like this :
Code:
--=[ Record 1 ]=-- -- -- -- -- --
CREATE TABLE ....
...

--=[ Record 2 ]=-- -- -- -- -- --


--=[ Record 3 ]=-- -- -- -- -- --


CREATE TABLE ...

--=[ Record 4 ]=-- -- -- -- -- --


CREATE TABLE ...

--=[ Record 5 ]=-- -- -- -- -- --
 
--=[ Record 6 ]=-- -- -- -- -- --


CREATE TABLE ...
/

CREATE TABLE ...
/

--=[ Record 7 ]=-- -- -- -- -- --
I would say, it is easier to follow.

And to get some useful information too, let us change the code into this :
Code:
awk -vRS=';' '{n=split($0,a,/CREATE/)-1;print"Record",NR,"has",n,"CREATEs -",n==1?"ok":"ERROR"}'
So the output will show which record is wrong :
Code:
Record 1 has 1 CREATEs - ok
Record 2 has 0 CREATEs - ERROR
Record 3 has 1 CREATEs - ok
Record 4 has 1 CREATEs - ok
Record 5 has 0 CREATEs - ERROR
Record 6 has 2 CREATEs - ERROR
Record 7 has 0 CREATEs - ERROR
Of course, you have to ignore the last error, as it is correct to have no CREATE after the last semi-colon. But of course, you could alter the code to hide the last error.

I hope I understood your goal correctly.


Feherke.
 

Sorry, got sucked into another issue with a higher priority... Will probably look at the solutions later on...

But Many thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top