Link to home
Start Free TrialLog in
Avatar of mhopkins9901
mhopkins9901Flag for United States of America

asked on

sed and regular expression

I have a 10000 line code file that has the following repeated about 500 times.

/* Present next screen */                                                      
             CHGVAR     VAR(&#VENDN) VALUE('Text1')                            
             CHGVAR     VAR(&#MSG01) VALUE('Text2')                                
             SNDF       RCDFMT(MSGCTL)                                          
             SNDRCVF    RCDFMT(SCRN02)                                          
                                                                               
/* Cancel was selected */                                                      
             IF         COND(&IN12 *EQ '1') THEN(GOTO CMDLBL(@CANMSG))  

I need to delete all occurrences of all the text form the  /* to the ending CMDLBL(@CANMSG)). The text is in VALUE('Text1')  and VALUE('Text2') are never the same.  

I know this can be done with sed but I can't figure out how to get the regex right. I end up selecting the whole file. Thanks for any help you can give with the reqex and sed

Avatar of Maciej S
Maciej S
Flag of Poland image

You mean from the second "/*"? Is there always "Cancel was selected" text?
sed '/\/\*/,/CMDLBL(@CANMSG))/d'
Avatar of mhopkins9901

ASKER

Yes "Cancel was selected" is always there.

 Do you think it would be less complicated if I did this in two steps.

I guess the "/*" and "*/" makes it a little harder when it shows up twice in the expression.
If "Cancel..." is static, you may use something like below:
sed '/\/\* Cancel was selected \*\//,/CMDLBL(@CANMSG))/d' file.txt

Open in new window

Do you want to delete all occurrences of all the text form the  /*
as in /* Present next screen */              
or from the /* Cancel was selected */         ?
Thanks for asking ozo.

I need to delete all occurrences of  all the text in the attached code snippet.
/* Present next screen */                                                       
             CHGVAR     VAR(&#VENDN) VALUE('Text1')                            
             CHGVAR     VAR(&#MSG01) VALUE('Text2')                                 
             SNDF       RCDFMT(MSGCTL)                                          
             SNDRCVF    RCDFMT(SCRN02)                                          
                                                                                
/* Cancel was selected */                                                       
             IF         COND(&IN12 *EQ '1') THEN(GOTO CMDLBL(@CANMSG))   

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So ozo's solution is working one, and you should forget mine one :)
But - it will delete everything from first "/*" up to first CMDLBL(@CANMSG)). Then it will start search "/*" again.
Do you have "/*" somewhere else in your file? If so - be careful, cause you may delete too much.
The best would be to have some sample file, not just text to delete. If you can, attach it here.