Commenting lines in a file using SED

Hi,

I need to comment the below lines in a file using sed.These are the few lines of the jsp file that need to be commented.

if(top.location != location){
      top.location.href = location.href;
}

Using the below command two lines can be commented:

if(top.location != location){
     top.location.href = location.href;
sed -i -e 's:if(top.location != location){://if(top.location != location){:' -i -e 
's:top.location.href = location.href;://top.location.href = location.href;:' header.jsp 

but I am not sure how to comment the line having } as there are many other lines having }.

.Is there a way I can merge lines top.location.href = location.href; and } then comment them?

It seems you're using GNU sed, so you could try something like this:

sed -i '/if(top.location != location){/{
N;N;/if(top.location != location){\ntop.location.href = location.href;\n}/ {
s|^|//|mg
  }
}' infile

after I ran this code my lines are not getting commented at all.and when I run this piece of code in my perl script.It gives me error

perl postinstall.pl 
sed: -e expression #1, char 67: unterminated address regex
#!/usr/local/bin/perl
&post_install_actions();
sub post_install_actions
 {
        $file = "/folder_struct/header.jsp";
        #`sed -i -e 's:if(top.location != location){://if(top.location != location){:' -i -e 's:top.location.href = location.href;://top.location.href = location.href;:' $file`;
 
`sed -i -e'/if(top.location != location){/{
N;N;/if(top.location != location){\ntop.location.href = location.href;\n}/ {
s|^|//|mg
  }
}' $file`;

}

This may do what you want if each section starts with "if(top.location" and ends with a curly brace at the begging of a line

 sed '/if(top.location/,/^}/s/^/\/\//' header.jsp

sed '/if(top.location/,/^}/s/^/\/\//' header.jsp does not work correctly.

comments all the lines in the files after the lines I want to comment.I
if(top.location != location){
top.location.href = location.href;
}
I only want to comment these three lines in the file.

this should do it,

# invoke sed with the -n option to suppress it's normal output
# ie. sed -n -f add_comments input_file > output_file

/^if(top.location != location){$/{
:comment_lines
{ s/^/\\\\/
p
n
/^}$/! b comment_lines
s/^/\\\\/
}
}
p

Hi Czk,

I am actually new to sed .Could you please elaborate ur answer.I did not get it.Cant I merge the }in the third line with the second line and then comment the whole line.

$str="if(top.location != location){\ntop.location.href = location.href;\n}time to go on \n\n way to life(ksudkd=!kjd\n\ndjdurr\n}" ;
if ($str =~ /.*(if\(top.location != location\)\{.?)(top\.location.href = location\.href;.?)(\}).*/s) {
$str = "//$1//$2//$3";
print "match\n" ;
print $str;
}
else
{
print "doesnot match";
}

cheers,
Devaraj Takhellambam

Why don't you do it all in Perl?

Try to add this code:

{

local ($^I, @ARGV, $/) = ('', 'your_jsp');

my $pattern = <<END;
if(top.location != location){
      top.location.href = location.href;
}
END

(my $new = $pattern) =~ s|^|//|gm;
 

  while (<>) {
   s/\Q$pattern/$new/gs;
   print;
   }
}

lots of ways to do that but you need to use the hold space. The catch is that sed stores the hold space as a single line with embedded \n's to represent the "real" newlines. so if we were to read the following three lines into holdspace...

line one
line two
last line

the the holdspace would look like this

line one\nline two\nline three

so ^ and $ only match the start and end on the entire hold space.

here's an example that has the same effect as my previous post

#find this pattern
/^if(top.location != location){$/{

#copy to holdspace h
:comment_lines
{
#get next line[INDENT] n
[/INDENT]#Append to hold space, this accumulates multiple lines in the hold space
#but the pattern space is still only the most recent line read in [INDENT] H
[/INDENT]#continue collecting lines until we find a } on a line of it's own[INDENT] /^}$/! b comment_lines
[/INDENT]#now that we've collected all the lines we want to comment in the hold space
# we swap them back into the pattern space so we can work on them.[INDENT] x
[/INDENT]#and here's the catch...
#handle the start (remember it's now only one line so ^ only matches once)[INDENT] s/^/\\\\/
[/INDENT]#then handle each of the new lines (note the explicit line break)[INDENT] s/\n/\
[/INDENT]\\\\/g }
}
p
#print the pattern space