Problems with SED

I have a group of xml files and I need to insert 3 parameters just after this line in each file:
---------------Pattern to be searched for-------------------------
<!--The following configuration is a test configuration-->

---------------Parameters to be added---------------------------
<Parameter>
<Name>NamedThreadsEnableLocking</Name>
<Value>true</Value>
</Parameter>

<Parameter>
<Name>Test1</Name>
<Value>true</Value>
</Parameter>

<Parameter>
<Name>Test2</Name>
<Value>true</Value>
</Parameter>

Can anyone help me implement this with the sed command? Thanks in advance.

Say the params to be added are put in param.txt.

sed -e '/<!--The following configuration is a test configuration-->/r param.txt' input.xml > output.new.xml

Hi Vino,

This is wonderful use of sed. I have another query on the same situation, What if i need to append or insert a few more lines BEFORE a particualr string.

How can i do that?

Cheers !!!

Read those few lines into a variable e.g. EXAMPLE.

Sed for that particular string, say it is "welcome to the unix forums".

sed -e "s_welcome to the unix forums_$EXAMPLE\n&_g" input.txt

Not tested.

Hi Vino,

Thank u so much, But i did not understand that :frowning: newbie here .

Here is what i m looking for :-

i have a file that has a word "[IPAddr]" mentioned in it.
i need to add contents of "append.txt" above this word in this file.
The word "[IPAddr]" can be any where in the file.

Thanks for your help once again.

sed -e "s_\[IPAddr\]_append.txt\n\[IPAddr\]_g" input.txt

Vino,

Thanks for that. But one problem.

It adds the word "append.txt", What i need to do is add the contents of append.txt in that position.

Thanks

Any assistance please !!

One of those days when nothing clicks ! :frowning:

Here is a shell script that will do the same thing.

#! /bin/ksh
> output.txt
while read line
do
  if [[ "$line" == *IPAddr* ]] ; then
    cat append.txt >> output.txt
  fi ;
  echo "$line" >> output.txt
done < input.txt

Thanks for that Vino,

This is what i did :-

now after i execute this script, the sms.conf file becomes completely balnk :frowning:

did i mess up some thing ?

Thanks once again

Ofcourse, you did.

I mentioned output.txt and input.txt. In your script, there is just one file - sms.conf

bash does not provide a way to perform read and write on a single file at the same time.

You output everything to a new file. And then later, rename it to the original file.

#! /bin/bash

> sms.conf.new
while read line
do
if [[ "$line" == *IPAddr* ]] ; then
cat append.txt >> sms.conf.new
fi ;
echo "$line" >> sms.conf.new
done < sms.conf

mv sms.conf.new sms.conf

Fantastic...You are too good Vino :slight_smile: My apologies for the mess up, m still getting used to this.

Thanks a lot once again. I hope i can bother you again if needed :wink:

Cheers !!!!