Commenting Multiple lines using Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Commenting Multiple lines using Shell Script
# 1  
Old 12-27-2012
Oracle Commenting Multiple lines in XML file using Shell Script

I have an xml file which has following code :

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)

Code:
TEMPFILE=replaceYY.tmp
file=hello.xml
sed -n '1h;1!H;${;g;s@<abc-ref.*hij*</abc-ref>@<!--Nothing-->@g;p;}' $file > $TEMPFILE
mv $TEMPFILE $file

But it is not working for me! Any help please..

Last edited by Dish; 12-27-2012 at 05:26 AM.. Reason: code tags
# 2  
Old 12-27-2012
How about something like:
Code:
sed '/abc-ref/,/abc-ref/s/^/# /' file

# 3  
Old 12-27-2012
Code:
sed '/abc-ref/,/abc-ref/s/^/# /' file

This code puts # in front of every line.
Firstly I want to comment only this section out, something like
Code:
<!--abc-ref> 
     <abc-name>abc.efg.hij.klm</abc-name> 
  </abc-ref-->

Since it is an xml file # will not work here.
Also there are more than one <abc-ref> tags, so I wanted to use "hij" string to make the shell script pattern unique.

---------- Post updated at 06:48 PM ---------- Previous update was at 01:52 PM ----------

Any pointers? This is quite an easy question for experts I guess! Smilie

Last edited by Dish; 12-27-2012 at 04:24 AM.. Reason: In reply to above code.
# 4  
Old 12-27-2012
Code:
sed '/abc-ref/,/abc-ref/s/<abc-ref/<!--abc-ref/;s/<\/abc-ref/<\/abc-ref--/' file

# 5  
Old 12-27-2012
But how to make sure the tag <abc-ref> with value
abc.efg.hij.klm is the only one getting commented out? SmilieSmilie
there are more than one <abc-ref> tags!
# 6  
Old 12-27-2012
Try this (relying on your sed's capability to interpret EREs):
Code:
$ sed -r '/<abc-ref>/ {N;N;/hij/ {s#(abc-ref)#!--\1#; s#(/abc-ref)#\1--#}}' file
 <abc-ref> 
     <abc-name>abc.efg.saw.klm</abc-name> 
  </abc-ref>
 <!--abc-ref> 
     <abc-name>abc.efg.hij.klm</abc-name> 
  </abc-ref-->
 <abc-ref> 
     <abc-name>abc.efg.yxz.klm</abc-name> 
  </abc-ref>

This User Gave Thanks to RudiC For This Post:
# 7  
Old 12-28-2012
Linux

Quote:
Originally Posted by RudiC
Try this (relying on your sed's capability to interpret EREs):
Code:
$ sed -r '/<abc-ref>/ {N;N;/hij/ {s#(abc-ref)#!--\1#; s#(/abc-ref)#\1--#}}' file
 <abc-ref> 
     <abc-name>abc.efg.saw.klm</abc-name> 
  </abc-ref>
 <!--abc-ref> 
     <abc-name>abc.efg.hij.klm</abc-name> 
  </abc-ref-->
 <abc-ref> 
     <abc-name>abc.efg.yxz.klm</abc-name> 
  </abc-ref>

And it worked!! SmilieSmilie
Thanks a lot buddy Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script count lines and sum numbers from multiple files

I want to count the number of lines, I need this result be a number, and sum the last numeric column, I had done to make this one at time, but I need to make this for a crontab, so, it has to be an script, here is my lines: It counts the number of lines: egrep -i String file_name_201611* |... (5 Replies)
Discussion started by: Elly
5 Replies

2. Shell Programming and Scripting

Commenting out a cron entry through a shell script

In my cron thare is a line like 24 11 * * * /usr/batch/bin/abc.sh > /usr/batch/log/abc.log 2>&1 along with other entries. I want to comment out this line through a shell script. My local variable 'line'ontains the full entry (i.e. 24 11 * * * /usr/batch/bin/abc.sh > /usr/batch/log/abc.log... (4 Replies)
Discussion started by: Soham
4 Replies

3. Shell Programming and Scripting

how to write multiple lines to a file using shell script?

I need to create an xml using shell script, but i first want to know how can i write multiple lines to file using shell script? (7 Replies)
Discussion started by: vel4ever
7 Replies

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

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

6. Shell Programming and Scripting

shell script: grep multiple lines after pattern match

I have sql file containing lot of queries on different database table. I have to filter specific table queries. Let say i need all queries of test1,test2,test3 along with four lines above it and sql queries can be multi lines or in single line. Input file contains. set INSERT_ID=1; set... (1 Reply)
Discussion started by: mirfan
1 Replies

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

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

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