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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting use shellscript to find the count of a line in a set of lines
# 1  
Old 01-24-2009
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 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>

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 .

Thanks alot in advance.....
# 2  
Old 01-27-2009
Use awk's range feature to execute a program that counts for every matching line. Here's one way to do it:
Code:
 awk '
  /^<template_file name="Approve External"/,/^<\/template_file>/      \
    { if (match($0,"<branch value=")) branches["approve"]++; }
  /^<template_file name="Workflow"/,/^<\/template_file>/      \
    { if (match($0,"<branch value=")) branches["workflow"]++; }
    { next; } 
  END \
    { for (x in branches) print x,branches[x]; }
'

Or something like that. You should be able to figure out the rest.
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 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

3. Shell Programming and Scripting

Want to count the number of lines after the first line

hi, How can i count the number of lines after the first line in a flat file in unix? Say i have a flat file with a header like: Student Name Student ID .... Tnx (7 Replies)
Discussion started by: reignangel2003
7 Replies

4. Shell Programming and Scripting

Count lines separated by new line

Hi guys, I have a file which has random records like mentioned below emcpower28a pci@3,03 (disk physical name) pci@3,04 emcpower9a pci@1,03 pci@2,03 pci@3,01 pci@4,03 there could be any number of disk names for any LUN (emc...) So, I want a solution to count disk names for its... (12 Replies)
Discussion started by: prashant2507198
12 Replies

5. Shell Programming and Scripting

Parse large file on line count (random lines)

I have a file that needs to be parsed into multiple files every time there line contains a number 1. the problem i face is the lines are random and the file size is random. an example is that on line 4, 65, 187, 202 & 209 are number 1's so there has to be file breaks between all those to create 4... (6 Replies)
Discussion started by: darbs121
6 Replies

6. Shell Programming and Scripting

shell to find the count fields of each line

hi, i've many unload files with delimiter '|'. I'm trying to load them to the specific tables from those unl's. The problem here is, some unl's are corrupted. To be exact, some files doesnt seem to have the exact number of fields as in the table. So im trying to identify the corrupted... (6 Replies)
Discussion started by: dvah
6 Replies

7. Shell Programming and Scripting

awk: sort lines by count of a character or string in a line

I want to sort lines by how many times a string occurs in each line (the most times first). I know how to do this in two passes (add a count field in the first pass then sort on it in the second pass). However, can it be done more optimally with a single AWK command? My AWK has improved... (11 Replies)
Discussion started by: Michael Stora
11 Replies

8. Shell Programming and Scripting

How to find lines that match exact input and count?

I am writing a package manager in BASH and I would like a small snippet of code that finds lines that match exact input and count them. For example, my file contains: xyz xyz-lib2.0+ xyz-lib2.0 xyz-lib1.5 and "grep -c xyz" returns 4. The current function is: # $1 is the package name.... (3 Replies)
Discussion started by: cooprocks123e
3 Replies

9. Shell Programming and Scripting

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 ... (2 Replies)
Discussion started by: millan
2 Replies

10. UNIX for Dummies Questions & Answers

find out the line count over FTP connection

I need help to find out the number of lines in a file which exists in remote machine. I the remote machine, auto login is enabled. So I do not need to worry about the username or password. But the problem is, I do not know how I can find out the line count once I get the FTP prompt. (1 Reply)
Discussion started by: Rita_questions
1 Replies
Login or Register to Ask a Question