Finding a match and deleting the line above to it..

Hi All,
I have a file with the data:

Sun is Hot
Moon is cool
;
 
-- Mon Sep 10 08:54:10 CDT 2012
-- Mon Sep 11 08:54:10 CDT 2012
-- Mon Sep 12 08:54:10 CDT 2012
revoke connect from SREE;
delete from = 'SREE';
grant connect to SREE with 'fastcar8';

I want to remove the line above the line "revoke connect from SREE;" if the above line starts with only "--" otherwise don't delete. This should continue till all the lines above the revoke line, those start with "--" got deleted. If any space or any other character comes it should stop deleting.

Finally my output should look like:

Sun is Hot
Moon is cool
;
 
revoke connect from SREE;
delete from = 'SREE';
grant connect to SREE with 'fastcar8';

Please help me with this.

Thanks!!

Sreenivas

Try

$awk '{if($0 ~ /^--/){A[++a]=$0}else if(a && $0 !~ /revoke connect from SREE/){for(i=1;i<=a;i++){print A[a]};a=0}else{print ;a=0}}' file
Sun is Hot
Moon is cool
;

revoke connect from SREE;
delete from = 'SREE';
grant connect to SREE with 'fastcar8';
awk 'END{if(p)printf "%s",p} /^--/{p=p $0 RS; next} /revoke connect from SREE/{p=x} p{$0=p $0; p=x}1' infile
1 Like

Hi Pamu,

Thanks for the reply!!

But with that code any other revoke connect lines also getting deleted (ex: revoke connect from CALU is also getting deleted).

My original file:

Sun is Hot
Moon is cool
;

-- Mon Sep 10 08:54:10 CDT 2012
-- Mon Sep 11 08:54:10 CDT 2012
-- Mon Sep 12 08:54:10 CDT 2012
revoke connect from SREE;
delete from = 'SREE';
grant connect to SREE with 'fastcar8';
 
-- Mon Sep 12 08:54:10 CDT 2011
revoke connect from CALU;
delete from = 'CALU';
grant connect to CALU with 'calendar';

After running the below command:

awk '{if($0 ~ /^--/){A[++a]=$0}else if(a && $0 !~ /revoke connect from SREE/){for(i=1;i<=a;i++){print A[a]};a=0}else{print ;a=0}}' file

Output is as follows:

Sun is Hot
Moon is cool
;

revoke connect from SREE;
delete from = 'SREE';
grant connect to SREE with 'fastcar8';
 
-- Mon Sep 12 08:54:10 CDT 2011
delete from = 'CALU';
grant connect to CALU with 'calendar';

In above ouput

revoke connect from CALU;

line is missing. That shouldn't happen. Please correct the code.

And i cann't redirect this output to a temporary file. These changes should happen to the original file itself.

Please help me in getting these two requirements solved.

Thanks,
Sreenivas

---------- Post updated at 09:21 PM ---------- Previous update was at 06:50 PM ----------

Hi Scrutinizer

Thanks for the reply!!

Your code is not working. Please suggest me the solution based on the above requirement.

Thanks,
Sreenivas

What isn't working? I get:

Sun is Hot
Moon is cool
;

revoke connect from SREE;
delete from = 'SREE';
grant connect to SREE with 'fastcar8';
 
-- Mon Sep 12 08:54:10 CDT 2011
revoke connect from CALU;
delete from = 'CALU';
grant connect to CALU with 'calendar';

What is your OS and version?

1 Like

Hi
Scrutinizer

Awesome!! Thanks alot!!
Your code is working perfect. Previously i used it wrongly. Now i have corrected my mistake. Its working fine.

But i have a concern about how to make the changes in the original file without redirecting stdout to some temporary new file.

Regards,
Sreenivas

Hi, you are welcome. What is wrong with redirecting to a new file? You can rename it to the old file afterwards when satisfied with the result. It is the safest way..

1 Like

With Sed..

$ uname -rs
SunOS 5.10
$ sed ':l
> /--/{
> /revoke connect from SREE/{
> D
> }
> N
> /--[^-]*--*/!{
> n
> }
> bl
> }' inputfile
Sun is Hot
Moon is cool
;

revoke connect from SREE;
delete from = 'SREE';
grant connect to SREE with 'fastcar8';

-- Mon Sep 12 08:54:10 CDT 2011
revoke connect from CALU;
delete from = 'CALU';
grant connect to CALU with 'calendar';
$

Thats okay..No problem..Thank you all for resolving my issue..