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';
$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';
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';
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 ----------
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';
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..
$ 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';
$