1 57 58 import java.io.*; 59 import java.util.*; 60 61 import org.apache.oro.text.*; 62 import org.apache.oro.text.regex.*; 63 64 73 public final class grep { 74 static int _file = 0; 75 76 public static final void main(final String [] args) { 78 MatchActionProcessor processor = new MatchActionProcessor(); 79 80 if(args.length < 2) { 81 System.err.println("Usage: grep <pattern> <filename>"); 82 System.exit(1); 83 } 84 85 try { 86 if(args.length > 2) { 87 processor.addAction(args[0], new MatchAction() { 90 public void processMatch(MatchActionInfo info) { 91 info.output.println(args[_file] + ":" + info.line); 92 } 93 }); 94 } else { 95 processor.addAction(args[0]); 98 } 99 } catch(MalformedPatternException e) { 100 System.err.println("Bad pattern."); 101 e.printStackTrace(); 102 System.exit(1); 103 } 104 105 for(_file = 1; _file < args.length; _file++) { 106 try { 107 processor.processMatches(new FileInputStream(args[_file]), System.out); 108 } catch(IOException e) { 109 System.err.println("Error opening or reading " + args[_file]); 110 e.printStackTrace(); 111 System.exit(1); 112 } 113 } 114 } 115 116 } 117 | Popular Tags |