Count lines between two patterns inside a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count lines between two patterns inside a file
# 1  
Old 06-18-2009
Count lines between two patterns inside a file

Hi,
Im doing a script to find the number of lines included inside a file newly. These lines are in between #ifdef FLAG1 and #else or #endif or #else and #endif.
I tried like this,
awk '/#ifdef Flag1/,/#e/{print}' aa.c | wc -l

awk '/#ifndef Flag1/,/#endif/{print}' aa.c | awk '/#else/,/#endif/{print}' | wc -l

But it considers the first #endif that follows #ifdef. In some cases there are some nested #ifdef.
Can some one help me to solve this?

Thanks,
Priya.
# 2  
Old 06-18-2009
Please Post the sample input and the output your expecting.

Regards

Ravi
# 3  
Old 06-18-2009
Sample Input:

#include <>
...
#ifdef FLAG1
#include <FLAG.h>
#ifdef OS
s=splnet()
#else
s=splx()
#endif
#else
s=splx(s)
#endif
#ifndef FLAG1
#include <>
splx(s)
#else
/* Some code */
#endif

Sample Output:

#ifdef FLAG1
#include <FLAG.h>
#ifdef OS
s=splnet()
#else
/* Some code */

Thanks
# 4  
Old 06-18-2009
panyam - We do not really need much more information then what he provided. Create test data and mess with it.

Priya - You can use sed, which is probably a bit easier. You have to use the -n otherwise it will send output twice. You can see a lot of info on sed at the following pages:
Famous Sed One-Liners Explained, Part I - good coders code, great reuse
Sed - UNIX Stream Editor - Cheat Sheet - good coders code, great reuse
http://www.oracle.com/technology/pub...laney_sed.html

Here is a solution for your issue below (followed by an issue you may have)
Code:
-bash-3.2$ cat test.txt
One
Two
Three
Four
Five
Orange
Red
Blue
Black
Yellow
Silver
-bash-3.2$ sed -n '/Four/,/Black/ p' test.txt
Four
Five
Orange
Red
Blue
Black
-bash-3.2$ sed -n '/Four/,/Black/ p' test.txt | wc -l
6

If you have two sets of matches, for instance, going with the above example, you had another 'Four'/'Black' further down the file. It will return both groups, not between the groups, but just the groups themselves.. for instance:

Code:
-bash-3.2$ cat test.txt
One
Two
Three
Four
Five
Orange
Red
Blue
Black
Yellow
Silver
Four
Hundred
Black
-bash-3.2$ sed -n '/Four/,/Black/ p' test.txt
Four
Five
Orange
Red
Blue
Black
Four
Hundred
Black
-bash-3.2$ sed -n '/Four/,/Black/ p' test.txt | wc -l
9

Which may be confusing, but it is correct. Hope this helps!
# 5  
Old 06-18-2009
Sorry!

Last edited by Rhije; 06-18-2009 at 04:21 AM.. Reason: For some reason it double posted! :(
# 6  
Old 06-18-2009
As Rhije said,

try the following :

Code:
sed '/#ifdef/,/#endif/ p'  input_file | wc -l

sed search for the pattern till the end and the last matched it will consider.

Rhije : I asked priya to post input as well as output expected , since we will get clear clarity ( than textual explanation).

Thanks
# 7  
Old 06-18-2009
Thanks to all of you,
But, the file which i've, has more than one "#ifdef FLAG1 #endif" block. I've to get the total count from every block. Between these blockes there 'll be some lines. I should not include those lines. The last script treats it as a single block Smilie

Thanks,
Sella.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print before and after lines then count of patterns

What i'm trying to do here is show X amount of lines before and after the string "serialNumber" is found. BEFORE=3 AFTER=2 gawk '{a=$0} {count=0} /serialNumber/ && /./ {for(i=NR-'"${BEFORE}"';i<=NR;i++){count++ ;print a}for(i=1;i<'"${AFTER}"';i++){getline; print ; count ++; print... (5 Replies)
Discussion started by: SkySmart
5 Replies

2. Shell Programming and Scripting

How to search multiple patterns and remove lines from a file?

Hi, I have a file content as below. Table : PAYR Displayed fields: 15 of 15 Fixed columns: 4 List width 0999... (4 Replies)
Discussion started by: shirdi
4 Replies

3. Shell Programming and Scripting

Awk to Count Multiple patterns in a huge file

Hi, I have a file that is 430K lines long. It has records like below |site1|MAP |site2|MAP |site1|MODAL |site2|MAP |site2|MODAL |site2|LINK |site1|LINK My task is to count the number of time MAP, MODAL, LINK occurs for a single site and write new records like below to a new file ... (5 Replies)
Discussion started by: reach.sree@gmai
5 Replies

4. Shell Programming and Scripting

count the number of occurring patterns in a file.

Hi, I have a file with a '|' pipe delimeter. I want to find number of counts for a particular pattern in particular field. Is it possible to do it in a single command? 1) want to find total number of "0" in field 4. 2) want to find total number of different records in field 4 ( similar to... (5 Replies)
Discussion started by: rudoraj
5 Replies

5. Shell Programming and Scripting

reading lines from a file between two search patterns

Hi, I am new to shell scripting and is working on a script to extract lines from a log file between two time stamps using awk command. After some research I used following command: awk '/01 Oct 2011/{p=1} /10 Oct 2011/{p=0} p' test.log >> tmp.log This works fine. But now i want to... (3 Replies)
Discussion started by: davidtd
3 Replies

6. UNIX for Advanced & Expert Users

Count number of unique patterns from a log file

Hello Everyone I need your help in fixing this issue., I have a log file which has data of users logging in to an application. I want to search for a particular pattern in the log ISSessionValidated=N If this key word is found , the above 8 lines will contain the name of the user who's... (12 Replies)
Discussion started by: xtechkid
12 Replies

7. Shell Programming and Scripting

Removing file lines that each match to a different patterns

I have a very large file (10,000,000 lines), that contains a sample id and a property of that sample. I have another file that contains around 1,000,000 lines with sample ids that I want to remove from the original file (create a new file without these lines). I know how to do this in Perl, but it... (9 Replies)
Discussion started by: Jo_puzzled
9 Replies

8. Shell Programming and Scripting

Searching patterns in 1 file and deleting all lines with those patterns in 2nd file

Hi Gurus, I have a file say for ex. file1 which has 3500 lines in it which are different account numbers and another file (file2) which has 230000 lines in it. I want to read all the lines in file1 and delete all those lines from file2 which has that same pattern as in file1. I am not quite... (4 Replies)
Discussion started by: toms
4 Replies

9. Shell Programming and Scripting

Delete lines inside a file.

hi.... I have a file having more then thousand lines. i want to remove selected lines in it. And also if there exists two duplicate lines, I want to delete one of them. Please help me with awk and shell. :confused: (8 Replies)
Discussion started by: tushar_tus
8 Replies

10. Shell Programming and Scripting

To find the count of records from tables present inside a file.

hi gurus, I am having a file containing a list of tables.i want to find the count of records inside thes tables. for this i have to connect into database and i have to put the count for all the tables inside another file i used the following loop once all the tablenames are inside the file. ... (1 Reply)
Discussion started by: navojit dutta
1 Replies
Login or Register to Ask a Question