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!

comment out selected lines

Status
Not open for further replies.

IMAUser

Technical User
May 28, 2003
121
CH

Hi,
I have a number of sql scripts and within those scripts I need to comment out certain sections.

So, if the file is like below
text...
text...
text...
Insert into table ....( )
values ( ...)
/
commit
/
text...
text..

I need to start commenting out the lines where the first word is INSERT and finish after COMMIT. The comments need to be SQL style. So the resultant file should be

text...
text...
text...
/* Insert into table ....( )
values ( ...)
/
commit
/
*/
text...
text..

Any suggestions on how I could do it.

Many thanks.
 
Hi

Code:
awk '/^Insert/{print"/*";i=1}$0=="commit"&&i{i++}1;$0=="/"&&i==2{print"*/";i=0}' /input/file

[gray]# or[/gray]

awk '/^Insert/{print"/*";i=0;do{print;getline;if(/commit/)i=1;i+=(i!=0)}while(i!=4);print"*/"}1' /input/file

Feherke.
 

I am sorry, I tried to use both the options and they both are falling over with errors. And I am not ablr to debug them.

Can you help again please.

The actual extract from my file is as below

ALTER TABLE SUNDAY$AH
ADD CONSTRAINT ADRAH_DEFAULT_BOOL_CHK CHECK (ADRAH_DEFAULT_BOOL IN ('0', '1'))
/
INSERT INTO SUNDAY$AH(ADRAH_ADRA_UID, ADRAH_DEFAULT_BOOL)
SELECT ADRA_ADRA_UID, ADRA_MUT_TSMP
FROM SUNDAY$A_OLD
/
COMMIT
/
ALTER TABLE SUNDAY$AH ..........


 
Hi

I see you are quite undecided regarding the letter cases, so first of all let us ensure that will not be a problem.
Code:
awk '[red]tolower($0)~[/red]/^[red]i[/red]nsert/{print"/*";i=1}[red]tolower([/red]$0[red])[/red]=="commit"&&i{i++}1;$0=="/"&&i==2{print"*/";i=0}' /input/file

[gray]# or[/gray]

awk '[red]tolower($0)~[/red]/^[red]i[/red]nsert/{print"/*";i=0;do{print;getline;if([red]tolower($0)=="[/red]commit[red]"[/red])i=1;i+=(i!=0)}while(i!=4);print"*/"}1' /input/file
Than please post [tt]awk[/tt]'s error message and [tt]awk[/tt]'s version.

My [tt]gawk[/tt] executes both codes correctly on your new sample text too.

Feherke.
 

I have used nawk and that did the trick. Thanks a lot. Now please can you explain what the script is doing.

I understand the general principle which is to look for the text insert and to make it case insensitive you have added the tolower. Print /* and then keep looking for the next commit, increment 2 lines and then print */

But would be helpful if you could explain the number logic you have in both of them.

Thanks a ton.

 
You better understand this ?
awk '
toupper($1)=="INSERT"{print "/*";++i}
{print}
toupper($1)=="COMMIT"&&i{i=0;print "*/"}
' /path/to/input.sql

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi

Code:
awk '
tolower($0)~/^insert/ {       [gray]# if current line starts with insert[/gray]
  print [i]"/*"[/i];                 [gray]# output the comment start[/gray]
  i=1                         [gray]# and note that found an insert[/gray]
}
tolower($0)==[i]"commit"[/i] && i {  [gray]# if current line is commit and already found an insert[/gray]
  i++                         [gray]# just note that found the comment too[/gray]
}
1;                            [gray]# output current line[/gray]
$0==[i]"/"[/i] && i==2 {             [gray]# if current line is / and already found both insert and commit[/gray]
  print [i]"*/"[/i];                 [gray]# output the comment end[/gray]
  i=0                         [gray]# and note that processing the last found insert finished[/gray]
}
' /input/file

[gray]# or[/gray]

awk '
tolower($0)~/^insert/ {       [gray]# if current line starts with insert[/gray]
  print [i]"/*"[/i];                 [gray]# output the comment start[/gray]
  i=0;                        [gray]# note that commit was not found yet[/gray]
  do {
    print;                    [gray]# output current line[/gray]
    getline;                  [gray]# read in the next line in place of the current[/gray]
    [b]if[/b] (tolower($0)==[i]"commit"[/i])[gray]# if current line is commit and already found an insert[/gray]
      i=1;                    [gray]# note that found the commit[/gray]
    i+=(i!=0)                 [gray]# increment the counter[/gray]
  } [b]while[/b] (i!=4);             [gray]# stepped 2 lines after the commit[/gray]
  print [i]"*/"[/i]                  [gray]# output the comment end[/gray]
}
1                             [gray]# output current line[/gray]
' /input/file

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top