Display first 10 lines of a textfile which contains a specific string

I need to display the first ten lines in this text file from date start which is 2014-08-01 to 2014-08-10

2014-08-01 06:13:14,Name,4.5,CustomUnit,CustomType
2014-08-02 06:13:14,Name,4.5,CustomUnit,CustomType
2014-08-03 06:13:14,Name,4.5,CustomUnit,CustomType
2014-08-04 06:13:14,Name,4.5,CustomUnit,CustomType
2014-08-05 06:13:14,Name,4.5,CustomUnit,CustomType
2014-08-06 06:13:14,Name,4.5,CustomUnit,CustomType
2014-08-07 06:13:14,Name,4.5,CustomUnit,CustomType
2014-08-08 06:13:14,Name,4.5,CustomUnit,CustomType
2014-08-09 06:13:14,Name,4.5,CustomUnit,CustomType
2014-08-10 06:13:14,Name,4.5,CustomUnit,CustomType
2014-08-11 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-01 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-03 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-01 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-05 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-01 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-07 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-01 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-09 07:13:14,Name,4.5,CustomUnit,CustomType
2014-08-02 07:13:14,Name,4.5,CustomUnit,CustomType


These are my codes so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//main
#include <iostream>
#include <string>
#include <fstream>
#include <dirent.h>
#include "LReader.h"

int main(int argc, char* argv[]) {

		// test  getLogs()
	std::string strDateStart = "2014-08-02";
	std::string strDateEnd = "2014-08-10";
	contents = l.getLogs(strDateStart, strDateEnd);
	std::cout << "File content is: " << std::endl;
	std::cout << contents << std::endl;
}
//LReader.h


#include <string>
#include <iostream>
#include <fstream>
#include <vector>

#include <unistd.h>
using namespace std;

#ifndef LOGREADER_H_
#define LOGREADER_H_

class LReader {

	public:
	LReader(const string & pth) : path(pth) { };
	string getLogs (const string & c, const string & d);

	private:
	string fContent;
	fstream logFile;
	string path;
	vector<string> lines;

};

#endif /* LOGREADER_H_ */

//LReader.cpp

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <fstream>
#include <dirent.h>
#include <sstream>
#include <errno.h>
#include "LReader.h"


#include <unistd.h>
using namespace std;



string LReader::getLogs (const string & c, const string & d){

	 ifstream logFile(path.c_str());


	 if (logFile.fail())
	 	    {
	 	        cout << "Can't open file" << endl;
	 	        void exit ();
	 	    }
	 string line2;

	 while (getline (logFile,line2) )
	 	    {
	 	        if (line2.find(c) != string::npos)
	 	            lines2.push_back(line2);

	 	    }

// this code below display the last 10 lines, but I need the FIRST 10 LINES
	  /*for ( std::string line2; std::getline(logFile,line2); )
	 	    	    {
	 	    	        lines2.push_back(line2);
	 	    	    }
	 	    	for ( std::size_t i = std::min(lines2.size(), std::size_t(5)); i < lines2.size(); ++i )
	 	    	    {
	 	    	        std::cout << lines2[i] << std::endl;
	 	    	    }*/

}
for ( std::size_t i = std::min(lines2.size(), std::size_t(5)); i < lines2.size(); ++i )
That does not do "last 10". That does "all, except the first 5".

You don't have to continue reading the file once you have found 10 lines.


PS. you already have a thread about your program here http://www.cplusplus.com/forum/unices/140803/
Last edited on
ooops sorry, it does not show the last 10 lines but it shows the last 5 lines from the text file tho,
UNIX flavors have wonderful text processing commands built in. You don't need to write your own:
sed -n -e '/2014-08-01/,$' | head -10

The sed command reads standard input and searches for the first line with 2014-08-01 in it, and print everything from that line forward. The head -10 command then prints just the first 10 lines.

@dhayden:
The sample input is not sorted by timestamps; the 2014-08-01 entries are spread all over.

However, I'll take your sed and raise:
grep -m 1 -A 9 "2014-08-01" stats_20140801_061314.log

The grep prints the first line with 2014-08-01 and quits (the -m 1). However, it also prints the next 9 lines that are after the matching line (the -A 9). Ten consequtive lines.

grep -E "2014-08-0[1-9]|2014-08-10" stats_20140801_061314.log | head -10

Match lines with date in range and show only the first 10. Order is not important. That is what the task requires (if it were not a C++ programming excercise, where the actual data is insignificant).
Topic archived. No new replies allowed.