Script to put block comment after finding regex in xml file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to put block comment after finding regex in xml file
# 1  
Old 01-10-2011
Script to put block comment after finding regex in xml file

hi,
i need my bash script to find regex in xml file.. and comment 2 lines before and after the line that contains regex.. can't use # needs to be <!-- at the beginning and --> and the end of the comment.

so eg..
Code:
first block

<filter>
        <filter-name>MyRegEx</filter-name>
        <filter-class>MyRegExblablabla</filter-class>
    </filter>

second block

<filter-mapping>
        <filter-name>MyRegEx</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

so output should be
Code:
first block
<!--
 <filter>
        <filter-name>MyRegEx</filter-name>
        <filter-class>MyRegExblablabla</filter-class>
    </filter>

-->
 
second block
 
 
<!-- 
<filter-mapping>
        <filter-name>MyRegEx</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

 -->

unfortunately since it's an xml file.. # is not funtioning..
obviously there is no problem to find regex.. .. i can also find the lines of regex.. the difficulty is to wrap around 2 lines before and after.. with <!-- and -->
Does anyone know how to do it? i have been working on it for quite some time now..

Thank you

Last edited by Scott; 01-11-2011 at 03:26 PM.. Reason: Please use code tags
# 2  
Old 01-10-2011
You want to comments out lines, try sed, using N to get the entire element in /<\([^ >]*\).*<\/\1>/, and then wrap it in comment markup s/.*/<!-- & -->/ or similar.
# 3  
Old 01-10-2011
Quote:
Originally Posted by DGPickett
You want to comments out lines, try sed, using N to get the entire element in /<\([^ >]*\).*<\/\1>/, and then wrap it in comment markup s/.*/<!-- & -->/ or similar.

what do you mean by ?

"using N to get the entire element in /<\([^ >]*\).*<\/\1>/, "
# 4  
Old 01-10-2011
Code:
awk '/<filter>/||/<filter-mapping>/ {print "<!--\n" $0;next} 
     /<\/filter>/||/<\/filter-mapping>/ {print $0 "\n-->";next}1 ' infile

# 5  
Old 01-11-2011
Quote:
Originally Posted by rdcwayx
Code:
awk '/<filter>/||/<filter-mapping>/ {print "<!--\n" $0;next} 
     /<\/filter>/||/<\/filter-mapping>/ {print $0 "\n-->";next}1 ' infile


thanks .. but i have lots of sections that look like this.. i only need to comment section that has MyRegEx
Code:
    <filter>
        <filter-name>something</filter-name>
        <filter-class>something</filter-class>
        <init-param>
            <param-name>filterKey</param-name>
            <param-value>remote</param-value>
        </init-param>
    </filter>
     
   
    <filter>
        <filter-name>MyRegEx</filter-name>
        <filter-class>MyRegExblabla</filter-class>
    </filter>


    <filter>
        <filter-name>something</filter-name>
        <filter-class>somethingelse</filter-class>
        <init-param>

            <param-name>filterKey</param-name>
            <param-value>something</param-value>
        </init-param>
    </filter>

        <filter-mapping>
            <filter-name>something</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>

    <filter-mapping>
        <filter-name>something</filter-name>
        <url-pattern>/servlet/something</url-pattern>
        <url-pattern>/servlet/something</url-pattern>
        <url-pattern>/servlet/something</url-pattern>
        <url-pattern>/servlet/something</url-pattern>
        <url-pattern>/servlet/something</url-pattern>
    </filter-mapping>


    <filter-mapping>
        <filter-name>something</filter-name>
        <url-pattern>/remote/*</url-pattern>
    </filter-mapping>


    <filter-mapping>
        <filter-name>something</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <filter-mapping>
        <filter-name>MyRegEx</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

Moderator's Comments:
Mod Comment Please use code tags

Last edited by Scott; 01-11-2011 at 03:27 PM.. Reason: Code tags, please...
# 6  
Old 01-11-2011
Using sed N to get an entire element. Let's say the element name is MsgBlk:
Code:
 
sed '
  /<MsgBlk[ >]/{
    :loop
    /<\/Msgblk>/!{
      $d
      N
      b loop
     }
    s/<MsgBlk[ >].*<\/MsgBlk>/<!--\
&\
-->/
   }
 '

Narrative:
Find the line with the opening element, and on it:
Set a branch target named loop.
If the element is not closed:
If at EOF, it is junk, delete it. Some sed are funny wih N at EOF.
Pile the next line at the end of the buffer.
Go back to check fo close of element.
Pick up just exactly this whole element, and where its ends were, put commenting markup and new lines around the while element.
# 7  
Old 01-11-2011
Quote:
Originally Posted by DGPickett
Using sed N to get an entire element. Let's say the element name is MsgBlk:
Code:
 
sed '
  /<MsgBlk[ >]/{
    :loop
    /<\/Msgblk>/!{
      $d
      N
      b loop
     }
    s/<MsgBlk[ >].*<\/MsgBlk>/<!--\
&\
-->/
   }
 '

Narrative:
Find the line with the opening element, and on it:
Set a branch target named loop.
If the element is not closed:
If at EOF, it is junk, delete it. Some sed are funny wih N at EOF.
Pile the next line at the end of the buffer.
Go back to check fo close of element.
Pick up just exactly this whole element, and where its ends were, put commenting markup and new lines around the while element.


i ran it like you have specified the output hasn't changed at all.. (substituted MsgBlk with my regex)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using :<<cut / cut to comment out block of bash script

I am using : << cut / cut to comment out block of code. Works fine on few lines of script, then it gives me this cryptic error when I try to comment out about 80 lines. The "warning " is at last line of script. done < results 169 echo "END read all positioning parameters" 170... (8 Replies)
Discussion started by: annacreek
8 Replies

2. Shell Programming and Scripting

Replacing part of XML code inside comment tags

Hello! I'd like to modify custom values in a XML config file between comment tags using bash script. <feature> <keyboardshortcut>C-m</keyboardshortcut> <option1>disabled</option2> <option2>enabled</option2> </feature> <!-- bash script features START --> <feature> ... (2 Replies)
Discussion started by: prism1
2 Replies

3. Shell Programming and Scripting

finding a block in a file and replace with another file block.

(1) Yes but how is this block different from the other 24? You will need this information in order to identify and replace this block correctly (out of the 25). Ans: The 1st line and last line of this block are unique from other block. The 1st line is “rem Subset Rows (&&tempName.*) and The... (1 Reply)
Discussion started by: Zaheer.mic
1 Replies

4. Shell Programming and Scripting

search a word in a xml file and print the out put

hi , i m having a html file and this file looks like this <ssl> <name>PIA</name> <enabled>true</enabled> <listen-port>39370</listen-port> </ssl> <log> <name>PIA</name> </log> <execute-queue> <name>weblogic.kernel.Default</name> ... (7 Replies)
Discussion started by: becksram123
7 Replies

5. Shell Programming and Scripting

regex/shell script to Parse through XML Records

Hi All, I have been working on something that doesn't seem to have a clear regex solution and I just wanted to run it by everyone to see if I could get some insight into the method of solving this problem. I have a flat text file that contains billing records for users, however the records... (5 Replies)
Discussion started by: Jerrad
5 Replies

6. Shell Programming and Scripting

Help using SED to comment XML elements

I'm trying to write a script to help automate some VERY tedious manual tasks. I have groups of fairly large XML files (~3mb+) that I need to edit. I need to look through the files and parse the XML looking for a certain flag contained in a field. If I find this flag (an integer value) I need... (4 Replies)
Discussion started by: J-Hon
4 Replies

7. Shell Programming and Scripting

get rid of xml comment by grep or sed

Hi, I would like to get rid of all comment in an xml file by grep or sed command: The content seem like this: <!-- ab cd ef gh ij kl --> Anyone can help? Thanks and Regards (3 Replies)
Discussion started by: RonLii
3 Replies

8. Shell Programming and Scripting

Finding a XML element and moving the file

Hi All, I am looking for a awk/shell which can find an element named REFERENCE in a XML file and check whether it is empty or not. If there is no value in the REFERENCE element then correspondingly move the file to some other folder. The Unix server is AIX version 4. Any inputs... (9 Replies)
Discussion started by: karansachdeva
9 Replies

9. Shell Programming and Scripting

How to put a comment at the begining of the sentence

I have a file shows as below. I would like to put # before CCCC. so how to do in Solaris. Here sed doesnot support -i AAAA BBBB CCCCC DDDDD EEEEE FFFFFF (1 Reply)
Discussion started by: madhusmita
1 Replies

10. Shell Programming and Scripting

Block Comment in Shell script

how to put multiline comments in a shell script like /* Some code */ in C language? (3 Replies)
Discussion started by: skyineyes
3 Replies
Login or Register to Ask a Question