Extracting lines between 2 strings only if the pattern patches


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting lines between 2 strings only if the pattern patches
# 1  
Old 04-21-2008
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:
Code:
 <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:
Code:
<init-param>
<param-name>/default/directory</param-name>
 <param-value>default</param-value>
 </init-param>

Please help me.

Thanks,
Simmy

Last edited by Yogesh Sawant; 04-22-2008 at 02:18 AM.. Reason: added code tags
# 2  
Old 04-22-2008
this is the same question that you asked here
# 3  
Old 04-22-2008
Your command does not work

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
# 4  
Old 04-22-2008
Code:
sed '/<\/init-param>/{G;}' filename >temp
nawk 'BEGIN{RS=""}
{
	if(index($3,"defaul")!=0)
		print
}' temp
rm temp

# 5  
Old 04-22-2008
using Perl:
Code:
#!/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:
Code:
perl find_default_tags.pl filename.xml

# 6  
Old 04-23-2008
Thanks to all

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

Last edited by Yogesh Sawant; 05-09-2008 at 04:43 AM.. Reason: added code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

2. Shell Programming and Scripting

Searching for a pattern and extracting records related to that pattern

Hi there, Looking forward to your advice for the below: I have a file which contains 2 paragraphs related to a particular pattern. I have to search for those paragraphs from a log file and then print a particular line from those paragraphs. Sample: I have one file with the fixed... (3 Replies)
Discussion started by: danish0909
3 Replies

3. Shell Programming and Scripting

Delete lines in file containing duplicate strings, keeping longer strings

The question is not as simple as the title... I have a file, it looks like this <string name="string1">RZ-LED</string> <string name="string2">2.0</string> <string name="string2">Version 2.0</string> <string name="string3">BP</string> I would like to check for duplicate entries of... (11 Replies)
Discussion started by: raidzero
11 Replies

4. Shell Programming and Scripting

Extracting the strings matching a pattern from a word

Hi All , I need to extract the strings that are matching with the pattern : CUST.<AnyStringOfAnyLength>.<AnyStringOfAnyLength> from a file and then write all these string into another file. e.g. If a file SOURCE contains following lines : IF(CUST.ABCD.EFGH==1) THEN CUST.ABCD.EFGH =... (7 Replies)
Discussion started by: swapnil.nawale
7 Replies

5. Shell Programming and Scripting

Extracting N lines match number X of a pattern

Hi All, as the title says I need to extract N lines after match number X of a pattern. e.g. 111 xxx xxx 111 yyy yyy 111 www www 111 zzz zzz I would like to extract the two lines after the second 111 occurrence. I tried with grep but I didn't find any options to do that. Any... (11 Replies)
Discussion started by: f_o_555
11 Replies

6. Shell Programming and Scripting

help extracting a matching pattern and next lines of match

Hi there, i'm having some problems just making an awk script (i've tried this way, but other way can be posible for sure), for the next file file.txt <register> <createProfile> <result>0</result> <description><!]></description> <msisdn>34661461174</msisdn> <inputOmvID>1</inputOmvID>... (6 Replies)
Discussion started by: vicious
6 Replies

7. Shell Programming and Scripting

extracting lines from a file1 which maches a pattern in file2

Hi guys, Can you help me in solving ths problem? I have two files file1 and file2 as following: ===FILE1==== >LOC21 MASSKFCTVLSLALFLVLLTHANSAELFSFNFQTFNAANLILQGNASVSSSGQLRLTEVKSNGEPKVASL VASFATAFTFNILAPILSNSADGLAFALVPVGSQPKFNGGFLGLFQNVTYDP >LOC05... (11 Replies)
Discussion started by: smriti_shridhar
11 Replies

8. Shell Programming and Scripting

extracting numbers from strings

Hello all, I am being dumb with this and I know there is a simple solution. I have a file with the follwing lines bc stuff (more)...............123 bc stuffagain (moretoo)............0 bc stuffyetagain (morehere)......34 failed L3 thing..............1 failed this... (2 Replies)
Discussion started by: gobi
2 Replies

9. Shell Programming and Scripting

Extracting the lines between 2 strings of a file

Hi, I have a sql file and i need to extract the table names used in the sql file using a unix script. If i can extract the lines between the keywords 'FROM' and 'WHERE' in the file, my job is done. can somebody tell me how to do this using a shell script. If u can just let me know, how to... (2 Replies)
Discussion started by: babloo
2 Replies

10. UNIX for Dummies Questions & Answers

Extracting strings

Hi, How do I extract the bytes size string from the ls -l command. (1 Reply)
Discussion started by: hugow
1 Replies
Login or Register to Ask a Question