40 import java.io.BufferedReader;
41 import java.io.IOException;
42 import java.nio.file.Files;
43 import java.nio.file.Paths;
45 import java.util.function.*;
46 import java.util.regex.Pattern;
47 import java.util.stream.Collector;
48 import java.util.stream.Collectors;
50 import static java.lang.Double.parseDouble;
51 import static java.util.stream.Collectors.*;
84 public static void main(String[] args) {
85 if (args.length < 2) {
88 try (BufferedReader br =
new BufferedReader(
89 Files.newBufferedReader(Paths.get(args[args.length - 1])))) {
91 List<String> header = Arrays.stream(br.readLine().split(
","))
92 .map(String::trim).collect(toList());
100 switch (args[2].toUpperCase()) {
115 Comparator<String> cmp
116 = Comparator.comparing(str ->
getCell(str, column),
117 String.CASE_INSENSITIVE_ORDER);
122 br.lines().sorted(isAsc ? cmp : cmp.reversed())
123 .forEach(System.out::println);
131 Predicate<String> pattern
132 = Pattern.compile(args[2]).asPredicate();
133 br.lines().filter(str -> pattern.test(
getCell(str, column)))
134 .forEach(System.out::println);
144 Collectors.groupingBy(str ->
getCell(str, column),
145 toCollection(TreeSet::new)))
146 .forEach((str,
set) -> {
147 System.out.println(str +
":");
148 set.forEach(System.out::println);
181 }
catch (IOException e) {
188 System.out.println(
"#-----Statistics in several passes-------#");
190 Comparator<String> comparator
191 = Comparator.comparing(
192 (String str) -> parseDouble(
getCell(str, column)));
195 "Max: " + br.lines().collect(maxBy(comparator)).
get());
199 "Min: " + br.lines().collect(minBy(comparator)).
get());
203 DoubleSummaryStatistics doubleSummaryStatistics
204 = br.lines().collect(summarizingDouble(
205 str -> parseDouble(
getCell(str, column))));
206 System.out.println(
"Average: " + doubleSummaryStatistics.getAverage());
207 System.out.println(
"Sum: " + doubleSummaryStatistics.getSum());
211 if (args.length != n) {
218 int column = header.indexOf(
name);
225 private static String
getCell(String record,
int column) {
226 return record.split(
",")[column].trim();
230 System.out.println(
"Usages:");
232 System.out.println(
"CSVProcessor sort COLUMN_NAME ASC|DESC FILE");
233 System.out.println(
"Sort lines by column COLUMN_NAME in CSV FILE\n");
235 System.out.println(
"CSVProcessor search COLUMN_NAME REGEX FILE");
236 System.out.println(
"Search for REGEX in column COLUMN_NAME in CSV FILE\n");
238 System.out.println(
"CSVProcessor groupby COLUMN_NAME FILE");
239 System.out.println(
"Split lines into different groups according to column "
240 +
"COLUMN_NAME value\n");
242 System.out.println(
"CSVProcessor stat COLUMN_NAME FILE");
243 System.out.println(
"Compute max/min/average/sum statistics by column "
246 Arrays.asList(str).forEach(System.err::println);
255 implements Collector<String, Statistics, Statistics> {
276 Comparator<String> cmp = Comparator.comparing(
310 StringBuilder sb =
new StringBuilder();
311 sb.append(
"#------Statistics------#\n");
314 sb.append(
"Sum = ").append(
getSum()).append(
"\n");
315 sb.append(
"Average = ").append(
average()).append(
"\n");
316 sb.append(
"#------Statistics------#\n");
317 return sb.toString();
337 public Function<Statistics, Statistics>
finisher() {
343 return EnumSet.of(Characteristics.IDENTITY_FINISH);