Skip to content

Instantly share code, notes, and snippets.

@MoriTanosuke
Created October 7, 2010 16:46
Show Gist options
  • Save MoriTanosuke/615432 to your computer and use it in GitHub Desktop.
Save MoriTanosuke/615432 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Grep {
public static void main(String[] args) throws IOException {
if(args.length != 2) {
System.err.println("Wrong parameter count!");
System.err.println("USAGE:\tjava Grep pattern file");
System.exit(-1);
}
String re = ".*" + args[0] + ".*";
String filename = args[1];
File file = new File(filename);
BufferedReader in = new BufferedReader(new FileReader(file));
if(file.exists()) {
String line = null;
int lineNumber = 1;
while((line = in.readLine()) != null) {
if (line.matches(re)) {
System.out.println("L" + lineNumber + ": " + line);
}
lineNumber++;
}
}
in.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment