Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to search for a line that contains log4j and take 7 lines before and 3 lines after the match.

grep -B7 -A3 "log4j" web.xml

After that I want to add comment tags before this paragraph and after it.

<!--
paragraph that i found by grep
-->

I wrote this script bellow:

search=`find . -name 'web.xml'`
text=`grep -B7 -A3 "log4j" $search`
sed -i "/$text/c $newparagraph" $search

It's not working. Is there any way to just add comment symbol not replace the paragraph?

What I want to the script to do:

  1. search for the paragraph
  2. append
  3. append --> at the end

Edit: This is the paragraph that am trying manipulate :

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>

<listener>
    <listenerclass>
        org.springframework.web.util.Log4jConfigListener
    </listener-class>
</listener>

This paragraph is part of many paragraphs! I want make it like this:

<!--
<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>

<listener>
    <listenerclass>
        org.springframework.web.util.Log4jConfigListener
    </listener-class>
</listener>
-->
share|improve this question
    
It's likely your script didn't work because 'log4j' occurs twice in the section of interest. I've tested my solution and it works for the example provided. –  Shawn J. Goff Mar 7 '11 at 13:35

1 Answer 1

up vote 0 down vote accepted

This would be better done with an XML library, but here it is in sed...

Sed has i and a commands for inserting and appending lines. I think the following script will help.

sed -n '/log4jConfigLocation/=' < test.test |\
while read index; do
    sed -i $search -e "$((index-1)) i\ <\!--" -e "$((index+8)) a\ -->" < $search
done

The first part, `sed -n '/log4jConfigLocation/=' will get the line numbers that have 'log4jConfigLocation'. The next part uses those line numbers in two commands: insert a line 1 line above, and append a line 8 lines below.

There is a better way using sed's hold buffer, but I'm not familiar enough with it; if you're interested, you can see this guide. Ultimately, though, if you're going to be working with this type of stuff regularly, you should check out XMLStarlet.

share|improve this answer
    
Thanks 4 replay ,, i have changed the procedure because sometimes XML file would be changed i mean it could be not always 8 line to the up or 3 line to the bottom.... –  moata_u Mar 7 '11 at 20:48
    
@moata_u I expected that to be a problem, which is why XMLStarlet would be good to look at. It's an XML tool that makes parsing this stuff much more robust. –  Shawn J. Goff Mar 7 '11 at 20:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.