0

for all files containing a line like:

 <class name="blahblahblah" foo="bar" fooz="baz">

I would like to add this line right afterwards.

 <cache usage="read-write">

The key text for searching is

  <class name=

I have access to a bash shell for this.

Thanks!

2
  • 1
    Please get hold of proper XML tools. Sed and grep aren't fit for handling XML; it may look like plain text, but looks are deceiving. Commented Sep 12, 2011 at 18:09
  • Thanks, larsmans. But, there is nothing specific to XML in this question. I simply need to add a single line of text at a certain point in the file. Commented Sep 12, 2011 at 18:11

2 Answers 2

2

Try this one:

sed -i '/<class name=*/ a\
<cache usage="read-write">' filename.xml

and remember it is just one single command. The -i option (--in-place) changes the given file (in place).

Sign up to request clarification or add additional context in comments.

Comments

0

Is Awk an option?

awk '{ print; if ($0 ~ "<class name=") print "<cache usage=\"read-write\">"; }'

(Though I still strongly object to using line-oriented tools on XML files. A decent XML toolset saves you a lot of trouble in the long run.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.