Problem manipulating text using grep

Posted by moata_u on Ask Ubuntu See other posts from Ask Ubuntu or by moata_u
Published on 2011-03-06T12:19:22Z Indexed on 2011/03/06 16:18 UTC
Read the original article Hit count: 209

Filed under:
|
|

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

© Ask Ubuntu or respective owner

Related posts about bash

Related posts about commands