Commenting lines in a file using SED


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Commenting lines in a file using SED
# 1  
Old 03-01-2010
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.

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

Using the below command two lines can be commented:
Code:
if(top.location != location){
     top.location.href = location.href;

Code:
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?

Last edited by meetu; 03-02-2010 at 03:07 AM.. Reason: Use code tags, please!
# 2  
Old 03-01-2010
It seems you're using GNU sed, so you could try something like this:

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

# 3  
Old 03-01-2010
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
Code:
perl postinstall.pl 
sed: -e expression #1, char 67: unterminated address regex

Code:
#!/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`;

}

Last edited by Yogesh Sawant; 03-01-2010 at 01:32 PM.. Reason: added code tags
# 4  
Old 03-01-2010
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
Code:
 sed '/if(top.location/,/^}/s/^/\/\//' header.jsp

# 5  
Old 03-02-2010
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.

Last edited by meetu; 03-02-2010 at 02:38 AM..
# 6  
Old 03-02-2010
Wrench

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
# 7  
Old 03-02-2010
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Commenting Multiple lines using Shell Script

I have an xml file which has following code : <abc-ref> <abc-name>abc.efg.hij.klm</abc-name> </abc-ref> I want to comment this whole section out and I have written the following script : (where "hij" is unique string in the file) TEMPFILE=replaceYY.tmp file=hello.xml sed -n... (6 Replies)
Discussion started by: Dish
6 Replies

2. Shell Programming and Scripting

perl: Help with commenting out specific lines

Hi I'm trying to comment out specific lines from /etc/fstab file, for simplicity I'm trying to use perl one liner but it errors out, Below is the Perl oneliner I'm using, perl -wlp -i -e 'BEGIN{$flag=0}if (!/root/) && (!/boot/) && (!/tmpfs/) ) {$flag =1;} elsif (/^$/) {$flag=0} if ($flag)... (2 Replies)
Discussion started by: mbak
2 Replies

3. UNIX for Dummies Questions & Answers

Commenting multiple lines

Hi, Can anyone let me know how to comment multiple lines in VI editor? Many thanks. Regards, Venkat. (3 Replies)
Discussion started by: venkatesht
3 Replies

4. Shell Programming and Scripting

Commenting lines in Shell script

Hi All, I know we can comment by using "#" .... I want to know... is there any way to comment a whole big script easily.... In a file i need to comment more than 15 lines ........ and check the script and un comment back. I am learning VI now so its taking lot of time to comment and un... (4 Replies)
Discussion started by: firestar
4 Replies

5. Shell Programming and Scripting

commenting out lines between two delimiters

Hi All, I am struggling to get my head around the following issue. I am having to comment out lines between two delimiters by placing an asterix in position 7 but retain all lines in the file and in the same order. so for example a file containing: ... ... DELIM1 ... ... DELIM2... (2 Replies)
Discussion started by: Bruble
2 Replies

6. Shell Programming and Scripting

Commenting lines

Hi can any body pls help me : I have a file Which Content is like following: p3:s1234:powerfail:/usr/sbin/shutdown -y -i5 -g0 >/dev/msglog 2<>/dev/msglog ca:3:respawn:/opt/GoldWing/currentPM/local/critagt > /dev/msglog 2<>/dev/msglog ca:3:respawn:/opt/GoldWing/currentPM/local/startcia.sh... (2 Replies)
Discussion started by: Aditya.Gurgaon
2 Replies

7. Shell Programming and Scripting

Commenting xml file lines

Hi , I have a XML file like this <dependency> <groupId>fr.xxxx.portail.ear</groupId> <artifactId>_xxxEAR</artifactId> <version>1.0.0-20xxxxx.xxxxx-x</version> <type>ear</type> </dependency> I need to comment single/multiple lines from XML file. How can i... (6 Replies)
Discussion started by: scorpio
6 Replies

8. Shell Programming and Scripting

Commenting a Line In a File

HI all I am working on a script, few details are as follows. I have one properties File and one script. The property file contains the JOBID which are to be executed and the Script runs these jobs ONE by ONE. After completing the JOB I need to comment that job in the property File. This is the... (3 Replies)
Discussion started by: Prashantckc
3 Replies

9. Shell Programming and Scripting

How to execute the rest of the code after commenting multiple lines?

Hi, As I have seen in this forum how to comment multiple lines in the script, but it does not work properly for me. It is blocking the code but it does not execute the rest of the codes. This is my code #! /usr/bin/ksh month='date +"m%"' : << Comments Block if || then echo "inc =... (12 Replies)
Discussion started by: Yamini Thoppen
12 Replies

10. UNIX for Dummies Questions & Answers

Commenting lines

How to comment a set of lines in a script? we use # to comment a single line , is there ant other cmd to comment a block? (2 Replies)
Discussion started by: rolex.mp
2 Replies
Login or Register to Ask a Question