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
# 8  
Old 01-11-2011
Results vary. I used a pretty rough but effective regex. Did you use an extended regex without telling sed?:
Code:
echo '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>
' | sed '
  /<filter[-maping]*[ >]/{
    :loop
    /<\/filter[-maping]*>/!{
      $d
      N
      b loop
     }
    s/<filter[-maping]*[ >].*<\/filter[-maping]*>/<!--\
&\
-->/
   }
 '
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>
-->

PS: For variant element regex, the second test, element not closed, s/b a back referencer, \(whatever\) I opened with, have I closed with that = \1 ?:
Code:
/<\(regex-name\)[ >].*<\/\1>/!{


Last edited by DGPickett; 01-11-2011 at 05:15 PM..
# 9  
Old 01-11-2011
Quote:
Originally Posted by Poki
i ran it like you have specified the output hasn't changed at all.. (substituted MsgBlk with my regex)

is there a way with sed to removed more than one set of lines in one line?

so i mean

sed ${firstElem},${lastIndex}d web.xml > web1.xml

this will delete lines between ${firstElem},${lastIndex}

i want in the same line to do somethinkg like this (doesn't work so far)


sed ${firstElem},${lastIndex}d ; ${secondElem},${lastIndex1}d web.xml > web1.xml


at the same time delete lines between ${firstElem},${lastIndex} and ${secondElem},${lastIndex1}d

if i don't do this in one line.. indexes for the second set change.. and i need to grep for them again.. so it would be really good if i could delete the two sets simulatiously..

thanks a lot

---------- Post updated at 06:29 PM ---------- Previous update was at 06:28 PM ----------

Quote:
Originally Posted by DGPickett
Results vary. I used a pretty rough but effective regex. Did you use an extended regex without telling sed?:
Code:
echo '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>
' | sed '
  /<filter[-maping]*[ >]/{
    :loop
    /<\/filter[-maping]*>/!{
      $d
      N
      b loop
     }
    s/<filter[-maping]*[ >].*<\/filter[-maping]*>/<!--\
&\
-->/
   }
 '
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>
-->

PS: For variant element regex, the second test, element not closed, s/b a back referencer, \(whatever\) I opened with, have I closed with that = \1 ?:
Code:
/<\(regex-name\)[ >].*<\/\1>/!{



trying a different way.. now..


is there a way with sed to removed more than one set of lines in one line?

so i mean

sed ${firstElem},${lastIndex}d web.xml > web1.xml

this will delete lines between ${firstElem},${lastIndex}

i want in the same line to do somethinkg like this (doesn't work so far)


sed ${firstElem},${lastIndex}d ; ${secondElem},${lastIndex1}d web.xml > web1.xml


at the same time delete lines between ${firstElem},${lastIndex} and ${secondElem},${lastIndex1}d

if i don't do this in one line.. indexes for the second set change.. and i need to grep for them again.. so it would be really good if i could delete the two sets simulatiously..

thanks a lot
# 10  
Old 01-13-2011
Well, sed will happily delete ranges with "/regex1/,/regex2/d", and you can do that as many times as you want (as long as one does not start within another).

Your xml is pretty well behaved, but I often normalize it so every element starts at the beginning of a line. That simplified subsequent parsing, so you do not need to worry about one line elements except like <xxx yyy="zzz" www="xxx" />. You could expand that, so it is entirely simply normal.

I often pile sed on sed in a pipeline. The sort of sed that loops and does N does not mix easily with those that do simple line filtering, so why not, pipes are free. You can even embed each sed command in an executable script file that begins in the line "#!/usr/bin/sed -f" (your path may vary) and execute the scripts.
# 11  
Old 01-13-2011
Code:
gawk 'BEGIN{RS="</filter>|</filter-mapping>"}
/>MyRegEx</{ $0="<!--\n"$0RT"\n-->" }1 ' file

# 12  
Old 01-14-2011
Quote:
Originally Posted by Poki
if i don't do this in one line.. indexes for the second set change.. and i need to grep for them again.. so it would be really good if i could delete the two sets simulatiously..

thanks a lot
If you use regex not line #, then line numbering is not an issue. Consider doing it like "diff -e" output: put the changes in reverse line number order.

sed numbers lines at input, so the numbers do not change on one pass, in any case.
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