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.
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.
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