Skip to content
Snippets Groups Projects
Commit d055270e authored by Shane Do's avatar Shane Do
Browse files

Add grep program

parent 8878ac76
No related branches found
No related tags found
No related merge requests found
File added
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Grep {
List<String> cmd = new ArrayList<>();
public Grep(String input) {
String cmd = "grep --color=always -w \"" + input + "\" GrepTest/test.rtf";
this.cmd.add("/bin/sh");
this.cmd.add("-c");
this.cmd.add(cmd);
}
public List<String> run_cmd() throws IOException {
// Runtime runtime = Runtime.getRuntime();
// Process process = runtime.exec(this.cmd);
ProcessBuilder prc_builder = new ProcessBuilder(cmd);
Process process = prc_builder.start();
BufferedReader input = new BufferedReader
(new InputStreamReader(process.getInputStream()));
String line;
List<String> res = new ArrayList<>();
System.out.println("Start reading logs!");
//Fix this to return count of line matches
while ((line = input.readLine()) != null) {
System.out.println(line);
res.add(line);
}
System.out.println("End file!");
System.out.println("-----------------------------");
return res;
}
}
File added
{\rtf1\ansi\ansicpg1252\cocoartf2638
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
\margl1440\margr1440\vieww11520\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0
Error: This is error 1 in log file
Error: This is error 2 in log file
Error: This is error 3 in log file
}
\ No newline at end of file
{\rtf1\ansi\ansicpg1252\cocoartf2638
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
\margl1440\margr1440\vieww11520\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0
\f0\fs24 \cf0 Error: This is an error for multi test case 1\
}
\ No newline at end of file
{\rtf1\ansi\ansicpg1252\cocoartf2638
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
\margl1440\margr1440\vieww11520\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0
\f0\fs24 \cf0 Error: This is a confirm for multi test case 2\
!}
\ No newline at end of file
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class main {
public static void main (String args[]) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println("Input regex matching for grep command: ");
String input = sc.nextLine();
List<String> output = new ArrayList<>();
Grep grep = new Grep(input);
output = grep.run_cmd();
//Fix this to return count of line mmatches
// for (int i=0; i<output.size(); i++) {
// System.out.println(output.get(i));
// }
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment