Created
February 14, 2018 23:43
-
-
Save anonymous/9cfc3d1b057f7a137ccbfb94bf4bfcef to your computer and use it in GitHub Desktop.
Rust code shared from the playground
This file contains 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
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