Skip to content

Instantly share code, notes, and snippets.

Created February 14, 2018 23:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/9cfc3d1b057f7a137ccbfb94bf4bfcef to your computer and use it in GitHub Desktop.
Save anonymous/9cfc3d1b057f7a137ccbfb94bf4bfcef to your computer and use it in GitHub Desktop.
Rust code shared from the playground
extern crate itertools;
use itertools::Itertools;
use std::{fs::File, io::{BufRead, BufReader, BufWriter, Write}};
fn main() {
let (infile, colname, repl, outfile) = std::env::args().skip(1).tuples().next().expect("Invalid args\n");
let mut lines = BufReader::new(File::open(infile).unwrap()).lines();
let hdr = lines.next().unwrap().unwrap();
let idx = hdr.split(",").position(|c| c == colname).expect("Invalid column.");
let mut out = BufWriter::new(File::create(outfile).unwrap());
writeln!(out, "{}", hdr).unwrap();
for l in lines.map(|l| {
l.unwrap().split(",").enumerate().map(|(i, c)| if i == idx { &repl } else { c }).join(",")}) {
writeln!(out, "{}", l).unwrap();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment