Skip to content

Instantly share code, notes, and snippets.

@Ophirr33
Last active February 14, 2018 21:05
Show Gist options
  • Save Ophirr33/55509d26db6b0bc7d042541303c2b8b9 to your computer and use it in GitHub Desktop.
Save Ophirr33/55509d26db6b0bc7d042541303c2b8b9 to your computer and use it in GitHub Desktop.
use std::io::prelude::*;
use std::io::{BufReader, BufWriter};
use std::fs::File;
fn main() {
let args: Vec<String> = std::env::args().collect();
assert_eq!(args.len(), 5, "Usage: ./tool <input.csv> <colum-name> <replacement-string> <output.csv>");
let br = BufReader::new(File::open(&args[1]).expect("No input file"));
let mut lines = br.lines().map(|s | s.unwrap().split(',').map(|s| s.to_string()).collect::<Vec<_>>()).peekable();
let col_idx = lines.peek().and_then(|v| v.iter().position(|s| s == &args[2])).expect("Could not find column");
let mut bw = BufWriter::new(File::create(&args[4]).expect("Could not open output file"));
writeln!(bw, "{}", lines.next().unwrap().join(",")).unwrap();
lines.for_each(|mut line| {
if line.len() > col_idx { line[col_idx] = args[3].clone() }
writeln!(bw, "{}", line.join(",")).unwrap();
});
bw.flush().unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment