BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
int lines = 0;
while (reader.readLine() != null) lines++;
reader.close();
Update: To answer the performance-question raised here, I made a measurement. First thing: 20.000 lines are too few, to get the program running for a noticeable time. I created a text-file with 5 million lines. This solution (started with java without parameters like -server or -XX-options) needed around 11 seconds on my box. The same with wc -l (UNIX command-line-tool to count lines), 11 seconds. The solution reading every single character and looking for '\n' needed 104 seconds, 9-10 times as much.
BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
int lines = 0;
while (reader.readLine() != null) lines++;
reader.close();
Update: To answer the performance-question raised here, I made a measurement. First thing: 20.000 lines are too few, to get the program running for a noticeable time. I created a text-file with 5 million lines. This solution (started with java without parameters like -server or -XX-options) needed around 11 seconds on my box. The same with wc -l (UNIX command-line-tool to count lines), 11 seconds. The solution reading every single character and looking for '\n' needed 104 seconds, 9-10 times as much.
Files.lines
Java 8+ has a nice and short way using NIO using Files.lines. Note that you have to close the stream using try-with-resources:
long lineCount;
try (Stream<String> stream = Files.lines(path, StandardCharsets.UTF_8)) {
lineCount = stream.count();
}
If you don't specify the character encoding, the default one used is UTF-8. You may specify an alternate encoding to match your particular data file as shown in the example above.
Read a text file and count the number of lines, stop at a specific character
Split a text and write different lines in different text file - Oracle Forums
Linux count number of lines a specific word occurs in a text file based on certain date and time stamp - Unix & Linux Stack Exchange
performance tuning - Determine the Number of Lines in a Text File - Mathematica Stack Exchange
Videos
Hello,
I am currently doing some coding homework and it's due at midnight and I'm stuck on this issue. I want to be able to count the number of lines in a text file and stop at a specific character. Here is an example of the text file I'll be reading. Technically it will be a .tgf file but this is what .tfg files look like in Notepad.
0 1 2 3 4 # 2 2 1 3 0 4
I want it to count the number of lines until the "#". We're working on creating our own sort of graph API so 0-4 are the vertices and the count should equal 5. "#" is the separator. The numbers below represent the edges in the graph and I also need to count them so the expected output for the number of edges should be 3. I at first thought that .usedelimiter would be what I needed but it has not been useful. Here is what I've tried:
import java.io.*;
import java.util.*;
public class readGraph{
public static void main (String[] args) throws FileNotFoundException {
File file = new File("../graphtest.txt");
Scanner fileScanner = new Scanner(file);
int count = 0;
while ( fileScanner.hasNextLine()){
count++;
fileScanner.nextLine();
if (fileScanner.nextLine() == "#"){
break;
}
}
System.out.println(count);
}
}You can use Measure-Object in powershell like this:
PS D:\temp> dir -Recurse *.txt | Get-Content | Measure-Object -Line
Which will return the following:
Lines Words Characters Property
----- ----- ---------- --------
168
To expand on maweeras answer above (sorry not enough rep to comment), you can search for multiple file extensions by passing a comma-delimited array to -Include.
So for example:
dir -Recurse -Include *.ts,*.tsx -Exclude *node_modules* | Get-Content | Measure-Object -Line
The -c grep option already does the count. So the result of the grep is a single number. Hence wc will of course only find one line. Just remove the wc altogether.
The problem with your approach is that you are looking for a pattern which contains [ and ]. These have a special meaning in regular expressions, namely they are "character lists" and match any character enclosed in them. So, performing
grep '[com.java.Name abc]' logfile
would match any line containing any of the characters a, b, c, e, j, m, o, v, N, the space, and the period ., regardless of their location on the line (which likely matches every line of the log file).
You need to escape the [ and ], as in
grep -c '\[com.java.Name abc\]' logfile
or - as pointed out by @terdon - use the -F flag:
grep -c -F '[com.java.Name abc]' logfile
If you want to look for occurences at a certain date, the mechanism depends. If you know the day, say 2020-06-14, it could be as easy as stating
grep -c '^2020-06-14.*\[com.java.Name abc\]' logfile
If you want to search based on the full timestamp, that approach would only work if you knew the exact moment as it is formatted in the logfile, as in
grep -c '^2020-06-14 13:48:12,442.*\[com.java.Name abc\]' logfile
which is unlikely because then you probably wouldn't need to count the occurences in the first place. In that case, you could try to adapt some of the following answers:
- Extracting between specific time lines from log file with grep and awk
- grep particular log entry greater than specific time
Lots of answers, but none of them leveraging this, so here is another.
null[_String] := Null
Length @ ReadList["data.txt", null @ String, NullRecords -> True]
On my system this is more than three times as fast as Rolf Mertig's CountLines, and a lot more concise as well.
If even one Null for every record is too much memory usage then read in blocks of e.g. 1000:
num[Longest[x__String], ___] := Length @ {x}
Tr @ ReadList["data.txt", num @@ Table[String, {1000}], NullRecords -> True]
A simple Mathematica-only solution is:
CountLines[file_String /; FileExistsQ[file]] :=
Module[{counter = 0, str = OpenRead@file},
While[ Read[str, Record, NullRecords -> True] =!= EndOfFile,
counter++
];
Close[str];
counter];
which is quite slow of course, so 123 MB (1978142 lines) needs 20 seconds in Mathematica 9 and 13 seconds in Mathematica 8. However, it only used 45 MB RAM at most (MaxMemoryUsed[]), so I guess you can easily count GB files.
I could not get Leonid's code to work on my Windows machine quickly, but if you can, that is of course better. Somehow Link technologies (JLink, NETLink,MathLink) will nearly always beat pure Mathematica. Unfortunately.
Another fast method on Windows is to use Gnu coreutils and then
AbsoluteTiming[
ReadList["!\"C:\\Program Files (x86)\\GnuWin32\\bin\\wc\" -l < " <>
"I:\\allfiles.txt", Number] // First]
returns
{0.154009, 1978142}