Skip to content

Instantly share code, notes, and snippets.

@oivoodoo
Created December 11, 2023 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oivoodoo/441bbb9c69f3c9b6daeb47848e6399d3 to your computer and use it in GitHub Desktop.
Save oivoodoo/441bbb9c69f3c9b6daeb47848e6399d3 to your computer and use it in GitHub Desktop.
use std::env;
use std::error::Error;
use std::fs::File;
use std::io::{BufRead, BufReader, BufWriter, Write};
use std::sync::{Arc, Mutex};
use rayon::prelude::*;
use regex::Regex;
fn delete_lines(input_file: &str, output_file: &str, pattern: &str) -> Result<(), Box<dyn Error>> {
let input = File::open(input_file)?;
let output = File::create(output_file)?;
let reader = BufReader::new(input);
let writer = Arc::new(Mutex::new(BufWriter::new(output)));
let regex = Regex::new(pattern)?;
reader
.lines()
.par_bridge()
.filter(|line| line.as_ref().map_or(false, |l| !regex.is_match(l)))
.for_each(|line| {
if let Ok(line_content) = line {
let mut local_writer = writer.lock().expect("Error acquiring lock");
writeln!(local_writer, "{}", line_content).expect("Error writing to output file");
}
});
Ok(())
}
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() != 4 {
eprintln!("Usage: {} <input_file> <output_file> <pattern>", args[0]);
std::process::exit(1);
}
let input_file = &args[1];
let output_file = &args[2];
let pattern = &args[3];
if let Err(err) = delete_lines(input_file, output_file, pattern) {
eprintln!("Error: {}", err);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_delete_lines() {
let input_file = "test_input.txt";
let output_file = "test_output.txt";
let pattern = "0000|1111";
// Write test data to the input file
let mut file = File::create(input_file).unwrap();
writeln!(file, "0000").unwrap();
writeln!(file, "2222").unwrap();
writeln!(file, "1111").unwrap();
writeln!(file, "3333").unwrap();
// Call the function to test
delete_lines(input_file, output_file, pattern).unwrap();
// Read the output file and check the contents
let file = File::open(output_file).unwrap();
let reader = BufReader::new(file);
let lines: Vec<String> = reader.lines().map(|line| line.unwrap()).collect();
assert_eq!(lines.len(), 2);
assert!(lines.contains(&"2222".to_string()));
assert!(lines.contains(&"3333".to_string()));
// Clean up
std::fs::remove_file(input_file).unwrap();
std::fs::remove_file(output_file).unwrap();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment