Trying to do a count on multiple lines in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trying to do a count on multiple lines in a file
# 1  
Old 02-16-2010
Trying to do a count on multiple lines in a file

Guys I am having a problem with being able to do a count of entries in a file. What I am trying to get a count of the total number of members that are listed in the files. So I need to pull the number of the lines after members. I tried using sed but it only seems to count the first line(members line) and not the actualy members themselves.

Here is the output of Config Data that I am pulling from:
Code:
pool Happy1 {
   monitor all happy1-check
   members
      1.1.1.1:https
      1.1.1.2:https
      1.1.1.3:https
      1.1.1.4:https
      1.1.1.5:https
      1.1.1.6:https
}
pool Happy2 {
   members
      1.1.2.4:https
}
pool Happy3 {
   monitor all happy3-check
   members
      1.1.3.4:https
}
pool Happy4 {
   members
      1.1.4.4:https
}
pool Happy5 {
   monitor all happy5-check
   members
      1.1.5.4:https
}


So for the above I am looking for it to return a value of 10.

Thanks in Advance.
# 2  
Old 02-16-2010
Code:
grep -c "members" file


Last edited by anbu23; 02-16-2010 at 04:50 PM..
# 3  
Old 02-16-2010
Unfortunately I can't search for anything specific in those lines cause it can be a port(80, 8080, etc...) or service name(https, dns, etc...)

Here is what I was working with and it works except for it pulls the members and the closed bracket } with it also. So I am getting incorrect data. I am not sure how to do it cleanly.

Code:
sed -n '/members/,/}/p' $CONF|wc -l

Code:
bash-3.00# sed -n '/members/,/}/p' input.txt |wc -l
     20

Code:
sed -n '/members/,/}/p' $CONF|more

Quote:
members
1.1.1.1:https
1.1.1.2:https
1.1.1.3:https
1.1.1.4:https
1.1.1.5:https
1.1.1.6:https
}
members
1.1.2.4:https
}
members
1.1.3.4:https
members
1.1.4.4:https
members
1.1.5.4:https
}
# 4  
Old 02-16-2010
Alternatively:
Code:
awk '/members/{c=1;next} /}/{c=0} c==1{tot++} END{print tot}' infile

# 5  
Old 02-16-2010
anbu23's grep will work for that sample data, but in case "https" may occur elsewhere, or in case a member need not have "https" in its line, the following may be of use:

Code:
$ awk '$1=="members" {m=1;next} $1=="}" {m=0} m {i++} END {print i}' data
10

Assumptions: the list of members begins on the line immediately following a line whose first word is "members" and is terminated by a line beginning with a "}" (with optional leading whitespace).

Regards,
alister
# 6  
Old 02-16-2010
Thx guys.

Alister, that was my assumption till I tried using your awk command. Yeah I found a wrinkle with the way the system writes the configuration file. So one version it is as listed above. Then an older version writes them as single line.

New code:
Code:
   members
      1.1.1.1:https
      1.1.1.2:https
      1.1.1.3:https

Older Code:
Code:
   members 1.1.1.1:https
   members 1.1.1.2:https
   members 1.1.1.3:https

# 7  
Old 02-16-2010
hey, scottzx7rr:

If i understand you correctly, you have two different formats to deal with. perhaps:

Code:
$ cat old
   members 1.1.1.1:https
             members 1.1.1.2:https
             members 1.1.1.3:https

$ ./scottzx7rr.sh old
       3

$ ### Note that i emptied the Happy2 members section for testing purposes

$ cat new
pool Happy1 {
   monitor all happy1-check
   members
      1.1.1.1:https
      1.1.1.2:https
      1.1.1.3:https
      1.1.1.4:https
      1.1.1.5:https
      1.1.1.6:https
}
pool Happy2 {
   members
}
pool Happy3 {
   monitor all happy3-check
   members
      1.1.3.4:https
}
pool Happy4 {
   members
      1.1.4.4:https
}
pool Happy5 {
   monitor all happy5-check
   members
      1.1.5.4:https
}

$ ./scottzx7rr.sh new
       9

$ cat scottzx7rr.sh
#!/bin/sh

# Try the old format.  if egrep finds nothing, then try the new
if ! egrep 'members ([0-9]+\.){3}[0-9]+' "$1"; then
        sed -n '/members/,/}/{/members/d;/}/d;p;}' "$1"
fi | wc -l


The egrep doesn't do a strict job of matching an ip address, but you can tighten that up if the code is of any use to you and you think it's required Smilie

Alister

Last edited by alister; 02-16-2010 at 07:45 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script count lines and sum numbers from multiple files

I want to count the number of lines, I need this result be a number, and sum the last numeric column, I had done to make this one at time, but I need to make this for a crontab, so, it has to be an script, here is my lines: It counts the number of lines: egrep -i String file_name_201611* |... (5 Replies)
Discussion started by: Elly
5 Replies

2. Shell Programming and Scripting

awk joining multiple lines based on field count

Hi Folks, I have a file with fields as follows which has last field in multiple lines. I would like to combine a line which has three fields with single field line for as shown in expected output. Please help. INPUT hname01 windows appnamec1eda_p1, ... (5 Replies)
Discussion started by: shunya
5 Replies

3. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

4. Shell Programming and Scripting

Count lines from multiple files (3)

Hey everyone, I've to count lines from string of files names then to show sum output of lines. for example: read x = F1 F2 F3 F1 = 12 lines F2 = 14 lines F3 = 10 lines = 36 what I did is: read x echo $x >|temp for x in $(cat temp) do wc -l < $x (3 Replies)
Discussion started by: Aviv
3 Replies

5. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

6. Shell Programming and Scripting

Multiple pattern matching using awk and getting count of lines

Hi , I have a file which has multiple rows of data, i want to match the pattern for two columns and if both conditions satisfied i have to add the counter by 1 and finally print the count value. How to proceed... I tried in this way... awk -F, 'BEGIN {cnt = 0} {if $6 == "VLY278" &&... (6 Replies)
Discussion started by: aemunathan
6 Replies

7. Solaris

WC -l does not count all the lines in a file? HELP

I have a file that I need to merge with another like file. Normally I remove the trailer reocrd and merge the file and update the trailer record of the second file. I did a WC -l on the first file before I removed the trailer record, and again afterwards. The count came back the same. I opened the... (6 Replies)
Discussion started by: Harleyrci
6 Replies

8. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

9. UNIX for Dummies Questions & Answers

trying to count lines in multiple files

Hi there, I need help. I want to run the command: less filename | wc -l But on multiple files in a directory So to get those files I would run ls -ltr | grep filename_2000123 or of course ls -ltr *filename_2000123* But I am having a problem running a loop to get a count of each... (1 Reply)
Discussion started by: llsmr777
1 Replies

10. Shell Programming and Scripting

count lines of file

dear all, i want to count the lines of a flat(text) file using awk.i have tried with {print NR} but its taking lot of time for a big file like 2GB file size. so i want better efficiency...so can any body please help me with some other and better awk code? Regards, Pankaj (15 Replies)
Discussion started by: panknil
15 Replies
Login or Register to Ask a Question