shellscript to find a line in between a particular set of lines of a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shellscript to find a line in between a particular set of lines of a text file
# 1  
Old 01-24-2009
shellscript to find a line in between a particular set of lines of a text file

i have a file a.txt and following is only one portion.
I want to search <branch value="/dev36/AREA/" include="yes"></branch> present in between
<template_file name="Approve External" path="core/approve/bin" and </template_file>
where the no of lines containing "<branch value= " is increasing



file contents:-

CTYPE available_templates SYSTEM './available_templates.dtd'>

<available_templates>

<template_file name="Approve External" path="core/approve/bin"
<command_list>
<command value="new_job"></command>
<command include="no" value="all"></command>
</command_list>
<role_list>
<role allusers="yes" include="yes" value="admin"></role>
<role allusers="yes" include="yes" value="master"></role>
<role allusers="yes" include="yes" value="editor"></role>
<role include="no" value="all"></role>
</role_list>
<branch_list>
<branch value="all" include="no"></branch>
<branch value="/devAREA/" include="yes"></branch>
<branch value="/upgrade_test/AREA/include="yes"></branch>
<branch value="/dev4/AREA/" include="yes"></branch>
<branch value="/dev36/AREA/" include="yes"></branch>
<branch value="/dev05/" include="yes"></branch>
<branch value="/mandsmoney/AREA/" include="yes"></branch>
</branch_list>
</template_file>
<template_file ="Workflow" path="core/sanj"
<command_list>
<command value="new_job"></command>
<command include="no" value="all"></command>
</command_list>
<role_list>
<role allusers="yes" include="yes" value="editor"></role>
<role allusers="yes" include="yes" value="admin"></role>
<role allusers="yes" include="yes" value="master"></role>
<role include="no" value="all"></role>
</role_list>
<branch_list>
<branch value="all" include="no"></branch>
<branch value="/devAREA/" include="yes"></branch>
<branch value="/upgrade_test/AREA/include="yes"></branch>
<branch value="/dev4/AREA/" include="yes"></branch>
<branch value="/dev36/AREA/" include="yes"></branch>
<branch value="/dev05/" include="yes"></branch>
<branch value="/mandsmoney/AREA/" include="yes"></branch>
</branch_list>

</template_file>

Please help me write a shellscript
# 2  
Old 01-24-2009
"where the no of lines containing "<branch value= " is increasing " Means??

Can you provide with the expected output of the input you provided.??
Is below what u expect??

$ cat inphtml | sed -n '/<template_file name="Approve External" path="core\/approve\/bin"/,/<\/template_file>/ {
> /<branch value="\/dev36\/AREA\/" include="yes"><\/branch>/p
> }
> '

Output:
<branch value="/dev36/AREA/" include="yes"></branch>

----------------------

$ cat inphtml | sed -n '/<template_file name="Approve External" path="core\/approve\/bin"/,/<\/template_file>/ {
> /<branch value=/p
> }
> '

Output:

<branch value="all" include="no"></branch>
<branch value="/devAREA/" include="yes"></branch>
<branch value="/upgrade_test/AREA/include="yes"></branch>
<branch value="/dev4/AREA/" include="yes"></branch>
<branch value="/dev36/AREA/" include="yes"></branch>
<branch value="/dev05/" include="yes"></branch>
<branch value="/mandsmoney/AREA/" include="yes"></branch>

Last edited by amicon007; 01-24-2009 at 02:08 AM..
# 3  
Old 01-24-2009
shellscript to find a line betwwen particular set of a lines

i have a file a.txt and following is only one portion.
I want to search <branch value="/dev36/AREA/" include="yes"></branch> present in between
<template_file name="Approve External" path="core/approve/bin" and </template_file>
where the no of lines containing "<branch value= " is increasing

CTYPE available_templates SYSTEM './available_templates.dtd'>

<available_templates>

<template_file name="Approve External" path="core/approve/bin"
<command_list>
<command value="new_job"></command>
<command include="no" value="all"></command>
</command_list>
<role_list>
<role allusers="yes" include="yes" value="admin"></role>
<role allusers="yes" include="yes" value="master"></role>
<role allusers="yes" include="yes" value="editor"></role>
<role include="no" value="all"></role>
</role_list>
<branch_list>
<branch value="all" include="no"></branch>
<branch value="/devAREA/" include="yes"></branch>
<branch value="/upgrade_test/AREA/include="yes"></branch>
<branch value="/dev4/AREA/" include="yes"></branch>
<branch value="/dev36/AREA/" include="yes"></branch>
<branch value="/dev05/" include="yes"></branch>
<branch value="/mandsmoney/AREA/" include="yes"></branch>
<branch value="/devAREA/" include="yes"></branch>
<branch value="/devAREA/" include="yes"></branch>
</branch_list>
</template_file>
<template_file ="Workflow" path="core/sanj"
<command_list>
<command value="new_job"></command>
<command include="no" value="all"></command>
</command_list>
<role_list>
<role allusers="yes" include="yes" value="editor"></role>
<role allusers="yes" include="yes" value="admin"></role>
<role allusers="yes" include="yes" value="master"></role>
<role include="no" value="all"></role>
</role_list>
<branch_list>
<branch value="all" include="no"></branch>
<branch value="/devAREA/" include="yes"></branch>
<branch value="/upgrade_test/AREA/include="yes"></branch>
<branch value="/dev4/AREA/" include="yes"></branch>
<branch value="/dev36/AREA/" include="yes"></branch>
<branch value="/dev05/" include="yes"></branch>
<branch value="/mandsmoney/AREA/" include="yes"></branch>
</branch_list>

</template_file>

<template_file ="township engagement" path="core/sanj/bills"
<command_list>
<command value="new_job"></command>
<command include="no" value="all"></command>
</command_list>
<role_list>
<role allusers="yes" include="yes" value="editor"></role>
<role allusers="yes" include="yes" value="admin"></role>
<role allusers="yes" include="yes" value="master"></role>
<role include="no" value="all"></role>
</role_list>
<branch_list>
<branch value="all" include="no"></branch>
<branch value="/devAREA/" include="yes"></branch>
<branch value="/upgrade_test/AREA/include="yes"></branch>
<branch value="/dev4/AREA/" include="yes"></branch>
<branch value="/dev36/AREA/" include="yes"></branch>
<branch value="/dev05/" include="yes"></branch>
<branch value="/mandsmoney/AREA/" include="yes"></branch>
<branch value="/devAREA/" include="yes"></branch>
</branch_list>

</template_file>

i mean to say the the number of lines in between <template_file name="Approve External" path="core/approve/bin"> and </template_file> is increasing,
the lines between <template_file ="township engagement" path="core/sanj/bills" and </template_file> is increasing
and also lines in between <template_file ="Workflow" path="core/sanj"> and </template_file> is also increasing.
The branch value is not fixed ,it can be be devAREA or upgrade_test dev4 ordev36 ordev05 or mandsmoney or anything as per requirment

Dear amincon007,
Thanks a lot for ur reply.

some portion of the file is given below.But the file format is same.

CTYPE available_templates SYSTEM './available_templates.dtd'>
<available_templates>
<template_file name="Approve External" path="core/approve/bin"
<command_list>
<command value="new_job"></command>
<command include="no" value="all"></command>
</command_list>
<role_list>
<role allusers="yes" include="yes" value="admin"></role>
<role allusers="yes" include="yes" value="master"></role>
<role allusers="yes" include="yes" value="editor"></role>
<role include="no" value="all"></role>
</role_list>
<branch_list>
<branch value="all" include="no"></branch>
<branch value="/devAREA/" include="yes"></branch>
<branch value="/upgrade_test/AREA/include="yes"></branch>
<branch value="/dev4/AREA/" include="yes"></branch>
<branch value="/dev36/AREA/" include="yes"></branch>
<branch value="/dev05/" include="yes"></branch>
<branch value="/mandsmoney/AREA/" include="yes"></branch>
<branch value="/devAREA/" include="yes"></branch>
<branch value="/devAREA/" include="yes"></branch>
</branch_list>
</template_file>
<template_file ="Workflow" path="core/sanj"
<command_list>
<command value="new_job"></command>
<command include="no" value="all"></command>
</command_list>
<role_list>
<role allusers="yes" include="yes" value="editor"></role>
<role allusers="yes" include="yes" value="admin"></role>
<role allusers="yes" include="yes" value="master"></role>
<role include="no" value="all"></role>
</role_list>
<branch_list>
<branch value="all" include="no"></branch>
<branch value="/devAREA/" include="yes"></branch>
<branch value="/upgrade_test/AREA/include="yes"></branch>
<branch value="/dev4/AREA/" include="yes"></branch>
<branch value="/dev36/AREA/" include="yes"></branch>
<branch value="/dev05/" include="yes"></branch>
<branch value="/mandsmoney/AREA/" include="yes"></branch>
</branch_list>
</template_file>
<template_file ="township engagement" path="core/sanj/bills"
<command_list>
<command value="new_job"></command>
<command include="no" value="all"></command>
</command_list>
<role_list>
<role allusers="yes" include="yes" value="editor"></role>
<role allusers="yes" include="yes" value="admin"></role>
<role allusers="yes" include="yes" value="master"></role>
<role include="no" value="all"></role>
</role_list>
<branch_list>
<branch value="all" include="no"></branch>
<branch value="/devAREA/" include="yes"></branch>
<branch value="/upgrade_test/AREA/include="yes"></branch>
<branch value="/dev4/AREA/" include="yes"></branch>
<branch value="/dev36/AREA/" include="yes"></branch>
<branch value="/dev05/" include="yes"></branch>
<branch value="/mandsmoney/AREA/" include="yes"></branch>
<branch value="/devAREA/" include="yes"></branch>
</branch_list>
</template_file>

i mean to say the the number of lines in between <template_file name="Approve External" path="core/approve/bin"> and </template_file> is increasing,
the lines between <template_file ="township engagement" path="core/sanj/bills" and </template_file> is increasing
and also lines in between <template_file ="Workflow" path="core/sanj"> and </template_file> is also increasing.
The branch value is not fixed ,it can be be devAREA or upgrade_test dev4 ordev36 ordev05 or mandsmoney or anything as per requirment

My aim is to check whether the string <branch value="/devarea/" include="yes"></branch> is present in
1)between <template_file name="Approve External" path="core/approve/bin"> and </template_file>
If it persent then how many times it is present in each case.
2)between <template_file ="Workflow" path="core/sanj"> and </template_file>
If it persent then how many times it is present in each case.
3)between <template_file ="township engagement" path="core/sanj/bills" and </template_file>
If it persent then how many times it is present in each case.

Please do reply how to write shellscript for this
If possible explain me the logic also
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find all lines in file such that each word on that line appears in at least n lines of the file

I have a file where every line includes four expressions with a caret in the middle (plus some other "words" or fields, always separated by spaces). I would like to extract from this file, all those lines such that each of the four expressions containing a caret appears in at least four different... (9 Replies)
Discussion started by: uncleMonty
9 Replies

2. UNIX for Dummies Questions & Answers

Ignore all lines except the --- dash line in a text file.

How do you write a script to ignore all lines except the --- dash lines and then remove --- dashes from the data in a text file? Also how do you separate data in a text file with a tab (for example, column1 (software) and column2 (date) ) ? Here is my scripts : I am getting errors in... (3 Replies)
Discussion started by: dellanicholson
3 Replies

3. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

4. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

5. UNIX for Advanced & Expert Users

Find and replace the line in text file

I have two files a.txt b.txt I want to find a line in a.txt and replace by another line from b.txt a.txt asfsdfsfsfdfsf asfwererfgdgf wrerwetretfdg b.txt werdfgdfgf werergfdgd sfdfgfgfgfgg i want to replace the 1st line of a.txt by 1st line of b.txt i want out put as (5 Replies)
Discussion started by: rammm
5 Replies

6. Shell Programming and Scripting

find digit which is greater than 1000 in text -using shellscript

Hi All, I am having an abc.txt , which contains some digits Eg:abc.txt 145 566 355 I want write shellscript in suchway that if any digit is greter than 1000 then it shuld display " text files contain digit, which is greater than 1000" Please help me to do so Thanks.. (8 Replies)
Discussion started by: pspriyanka
8 Replies

7. UNIX for Dummies Questions & Answers

find uniq lines in file, using the first field of line

Hello all, new to unix and have just found the forum. I think I will be here quite often, and hope that in time i will be able to provide soem help, role on not being a newbie anymore :) I have a question which iI am hoping someone could help me with. If i have a file with lines in in thus... (8 Replies)
Discussion started by: grom
8 Replies

8. Shell Programming and Scripting

Find lines in text file with certain data in first field

Hi all, Sorry for the title, I was unsure how to word my issue. I'll get right to the issue. In my text file, I need to find all lines with the same data in the first field. Then I need to create a file with the matching lines merged into one. So my original file will look something like... (4 Replies)
Discussion started by: rstev39147
4 Replies

9. Shell Programming and Scripting

use shellscript to find the count of a line in a set of lines

I have a file a.xml some portion of the file is given below.But the file format is same. CTYPE available_templates SYSTEM './available_templates.dtd'> <available_templates> <template_file name="Approve External" path="core/approve/bin" <command_list> <command... (1 Reply)
Discussion started by: millan
1 Replies

10. Shell Programming and Scripting

How to print the lines after 2nd line from text file

Hi All, I need to print the lines after 2nd line irrespective of the number of lines in file if the file contents are like below ---------- root:!:0:0::/root:/usr/bin/ksh daemon:!:1:1::/etc: bin:!:2:2::/bin: sys:!:3:3::/usr/sys: adm:!:4:4::/var/adm: uucp:!:5:5::/usr/lib/uucp:... (6 Replies)
Discussion started by: ram1729
6 Replies
Login or Register to Ask a Question