Extracting lines between 2 strings only if the pattern patches

Hi Friends,

Please help me with the following problem:
I have an xml file with the following lines:

 <init-param>
      <param-name>/default/directory</param-name>
      <param-value>default</param-value>
    </init-param>
    <init-param>
      <param-name>/default/logoutpage</param-name>
      <param-value>www.test.com</param-value>
    </init-param>
    <init-param>
      <param-name>/default/changepassworddate</param-name>
      <param-value>60</param-value>
    </init-param>
    <init-param>
      <param-name>/default/itemsperpage</param-name>
      <param-value>10</param-value>
    </init-param>
    <init-param>
      <param-name>/default/title</param-name>
      <param-value>xxxx</param-value>
    </init-param>
    <init-param>
      <param-name>/default/autopopulate</param-name>
      <param-value>yes</param-value>
    </init-param>

I need to write a script that will search for the word "default" within these lines and extract the complete tag starting and ending with:

<init-param>
<param-name>/default/directory</param-name>
 <param-value>default</param-value>
 </init-param>

Please help me.

Thanks,
Simmy

this is the same question that you asked here

Hi Yogesh,

The command:
sed '/param-/s/default/different/' file1 > file2
does not work.

Let me frame my question correctly.
I want to get all the lines starting from <init-param> and ending with </init-param> only if the lines between these tags has a "default" keyword.

Hope you can help me.

Thanks,
Simmy

sed '/<\/init-param>/{G;}' filename >temp
nawk 'BEGIN{RS=""}
{
	if(index($3,"defaul")!=0)
		print
}' temp
rm temp

using Perl:

#!/usr/local/bin/perl
# find_default_tags.pl
my $filename = shift;
open (X_FILE, '<', $filename)  or  die "Failed to read file $filename : $!";
{
    local $/;
    while (<X_FILE>) {
        while (m#(<init-param>.*?>default<.*?</init-param>)#gs) {
            print $1 . "\n";
        }
    }
}
close (X_FILE);

run this scritpt as:

perl find_default_tags.pl filename.xml

Thank you Cherry your code did work but with a small change, i changed the $3 to $2 only then i got all the occurances of the <init-param> which has default keyword, with $3 i would get only one occurance.

Yogesh - i am not very good at understanding perl so, i havent been able to try your code. Thanks anyway for the help.

I have one more question to add to this. Actually after getting the output for the list of <init-param> that has default keyword, i need to copy the output to another web.xml file again by searching for the default keyword, but, i want to copy these lines in the web.xml only after the last occurance of the keyword, default within the <init-param>.

So, say i have a file web1.xml from which i used the Sed command given by Cherry and got the output as :

<init-param>
      <param-name>/default/logoutpage</param-name>
      <param-value>https://test.com</param-value>
    </init-param>
    <init-param>
      <param-name>/default/changepassworddate</param-name>
      <param-value>60</param-value>
    </init-param>
    <init-param>
      <param-name>/default/itemsperpage</param-name>
      <param-value>10</param-value>
    </init-param>
    <init-param>
      <param-name>/default/title</param-name>
      <param-value>test</param-value>
    </init-param>
    <init-param>
      <param-name>/default/autopopulate</param-name>
      <param-value>yes</param-value>
    </init-param>
    <init-param>
      <param-name>/default/ncpath</param-name>
      <param-value>https://test@test.com</param-value>
    </init-param>

Now i want to paste the above output to another file web2.xml, which also has few <init-param> </init-param> tags, with default keyword. I want to search for the last occurance of the <init-pram>default</init-param> tag which has default keyword and just paste the above output after the last occurance.

Hope Cherry can help me with this also. Actually i am new to SED and AWK commands in unix and am expected by my manager to prepare a script like this, which i cannot untill unless i read the tutorials.

Thanks for the help and waiting for some more help