Last active
February 14, 2018 21:05
-
-
Save Ophirr33/55509d26db6b0bc7d042541303c2b8b9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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