/* * [Extracti.java] * * Summary: extract lines from text files that match given strings with case-insensitive compare. * * Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.1 2009-02-27 all switch means all strings must match */ package com.mindprod.extract; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * extract lines from text files that match given strings with case-insensitive compare. Not Regex. * * @author Roedy Green, Canadian Mind Products * @version 1.1 2009-02-27 all switch means all strings must match * @since 2009-02-26 */ public class Extracti extends Extract { /** * Determine if this line is wanted.. * * @param line line from the file * * @return true if this line is wanted, to be printed */ boolean isLineWanted( final String line ) { final String lcLine = line.toLowerCase(); if ( allMustMatch ) { for ( String s : stringsToSearchFor ) { if ( !lcLine.contains( s ) ) { return false; } } for ( Pattern p : regexesToSearchFor ) { final Matcher m = p.matcher( line ); if ( !m.find() ) { return false; } } return true; } else { for ( String s : stringsToSearchFor ) { if ( lcLine.contains( s ) ) { return true; } } for ( Pattern p : regexesToSearchFor ) { final Matcher m = p.matcher( line ); if ( m.find() ) { return true; } } return false; } } /** * Extracts lines in files that contain a given string. * * @param args strings to search for, then a -, then names of files to process, no wildcards. * strings are case-insensitive, not regexes. * -all switch means all strings must match * -where switch means display where each line found file/line # */ public static void main( String[] args ) { try { parse( args ); for ( int i = 0; i < stringsToSearchFor.length; i++ ) { stringsToSearchFor[ i ] = stringsToSearchFor[ i ].toLowerCase(); } new Extracti().processFiles(); } catch ( Exception e ) { displayError( e, "extracti", args ); } } // end main } // end Extract