Counting lines between words for multiple data sets

Hi everyone,
I know there have been slightly similar posts for counting the number of lines between specific words, but none of them were quite what I'm looking for.

I have a .txt file with all my data, and I've set it up so it looks something like this:
1
2
3
4
5
6
7
8
9
Line
 (data)
 (data)
 (data)
End
Line
 (data)
 (data)
End


and etc. So what I've been struggling to find is a way to count the number of lines in between 'Line' and 'End' for each set. I of course do not want to do this by hand as there are 1000 data sets. There are also some data sets that are empty, and so would appear like:

1
2
Line
End


Is there a way to do this?
question3099 wrote:
Is there a way to do this?


Of course.

Open the file, while(has next line) read line, if it's Line, start new count, if it's end, display count, otherwise count++, read everything between the newline character as a line, so
1
2
3
4
Line\n
 data\n
 data\n
End\n

read everything into a buffer until you reach the newline character, then check to see if that buffer is either line or end.

If you need help with any individual step, let us know.
Thanks, Intrexa,

I've almost got it working, but I can't get the count to work properly.

I've changed the data so that it now looks like this:
1
2
3
4
5
6
7
8
9
List
1
1
1
Cut
List
1
1
Cut


and so on. So really, I just need to count the number of 1's between each set of List and Cut.

Here's the important parts of what I've got so far:
1
2
3
4
5
6
7
8
9
10
11
int mycount;
  ifstream file("log5.txt");
  string line;
    while (getline(file, line)){
      if (line == "List"){
        mycount = int count("List","Cut","1");
        cout << count << "\n"; //for testing
      }
      if (line == "Cut")
        line++;
     }


What am I doing wrong here? I'm getting an error message that count is not defined in the current scope.

Thanks again.
Last edited on
mycount = int count("List","Cut","1");

What is this supposed to be? I can't tell if you're declaring a function or calling one.
Last edited on
Thanks L B,

I need to declare it first, and then print out its values with the cout in the next line. Here is my code after a few small changes:

1
2
3
4
5
6
7
8
9
10
 ifstream file("log5.txt");
  string line;
    while (getline(file, line)){
      if (line == "List"){
        int mycount = count("List","Cut","1");
        cout << mycount << "\n"; //for testing
      }
      if (line == "Cut")
        line++;
     }


But alas, it still returns Error: Function count("List","Cut","1") is not defined in current scope.
Last edited on
Have you defined the function count()? What did you expect to happen? You're trying to call a function that doesn't exist, so of course it's not defined in this scope.
Last edited on
Further up in the code - in a part I didn't copy over - I also have:
1
2
#include <algorithm>
  using namespace::std;


Isn't count() defined in algorithm?
http://www.cplusplus.com/reference/algorithm/count/ Yes, but you're using it wrong.

using namespace::std; this is clearly wrong. It should be using namespace std; without the scope resolution operator.
Thanks for the help LB,

After a bit more searching around on my own I found this beautiful solution on the internet and it worked perfectly after I trimmed my log5.txt file a bit more:
awk ' /List/ { if ( f!="") {print f-1}; f=0;f1=1 } f1 { f++ } ' log5.txt > newlog5.txt

Adapted from: http://www.unix.com/shell-programming-scripting/128999-counting-lines-between-two-patterns.html
Topic archived. No new replies allowed.