Comment

Comment by IshKebab

Yeah the existing wheels suck so I don't blame them for reinventing it. Often it is easier to write a proper program to do exactly what you want than to figure out the arcane `sed | xargs` command from the 70s that nobody has ever bothered to make vaguely usable.

Here's a program I wrote to count matches in a very large file. I wonder how long it would take to work out the equivalent bash command.

  use aho_corasick::AhoCorasick;
  use std::fs::File;
  use std::io::BufReader;
  fn main() {
    let args = std::env::args().collect::<Vec<_>>();
    if args.len() != 3 {
        eprintln!("Usage: ./count_occurances 'pattern' <filename>");
        return;
    }
    let f = File::open(&args[2]).expect("Error opening input file");
    let reader = BufReader::new(f);
    let patterns = &[&args[1]];
    let ac = AhoCorasick::new(patterns);
    let count = ac.stream_find_iter(reader).count();
    println!("{}", count);
  }
parent IshKebab 4 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton