Is there a Pattern Matching Utility like GREP in Windows?

前端 未结 30 1021
不知归路
不知归路 2020-12-04 06:23

Is there a similar utility to grep available from the Windows Command Prompt, or is there a third party tool for it?

30条回答
  •  离开以前
    2020-12-04 06:53

    I know that it's a bit old topic but, here is another thing you can do. I work on a developer VM with no internet access and quite limited free disk space, so I made use of the java installed on it.

    Compile small java program that prints regex matches to the console. Put the jar somewhere on your system, create a batch to execute it and add the folder to your PATH variable:

    JGrep.java:

    package com.jgrep;
    
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class JGrep {
    
        public static void main(String[] args) throws FileNotFoundException, IOException {
            int printGroup = -1;
            if (args.length < 2) {
                System.out.println("Invalid arguments. Usage:");
                System.out.println("jgrep [...-MODIFIERS] [PATTERN] [FILENAME]");
                System.out.println("Available modifiers:");
                System.out.println(" -printGroup            - will print the given group only instead of the whole match. Eg: -printGroup=1");
                System.out.println("Current arguments:");
                for (int i = 0; i < args.length; i++) {
                    System.out.println("args[" + i + "]=" + args[i]);
                }
                return;
            }
            Pattern pattern = null;
            String filename = args[args.length - 1];
            String patternArg = args[args.length - 2];        
            pattern = Pattern.compile(patternArg);
    
            int argCount = 2;
            while (args.length - argCount - 1 >= 0) {
                String arg = args[args.length - argCount - 1];
                argCount++;
                if (arg.startsWith("-printGroup=")) {
                    printGroup = Integer.parseInt(arg.substring("-printGroup=".length()));
                }
            }
            StringBuilder sb = new StringBuilder();
            try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
                sb = new StringBuilder();
                String line = br.readLine();
    
                while (line != null) {
                    sb.append(line);
                    sb.append(System.lineSeparator());
                    line = br.readLine();
                }
            }
            Matcher matcher = pattern.matcher(sb.toString());
            int matchesCount = 0;
            while (matcher.find()) {
                if (printGroup > 0) {
                    System.out.println(matcher.group(printGroup));
                } else {
                    System.out.println(matcher.group());
                }
                matchesCount++;
            }
            System.out.println("----------------------------------------");
            System.out.println("File: " + filename);
            System.out.println("Pattern: " + pattern.pattern());
            System.out.println("PrintGroup: " + printGroup);
            System.out.println("Matches: " + matchesCount);
        }
    }
    

    c:\jgrep\jgrep.bat (together with jgrep.jar):

    @echo off
    java -cp c:\jgrep\jgrep.jar com.jgrep.JGrep %*
    

    and add c:\jgrep in the end of the PATH environment variable.

    Now simply call jgrep "expression" file.txt from anywhere.

    I needed to print some specific groups from my expression so I added a modifier and call it like jgrep -printGroup=1 "expression" file.txt.

提交回复
热议问题