Skip to content

Instantly share code, notes, and snippets.

@kardeiz
Created July 21, 2015 17:38
Show Gist options
  • Save kardeiz/21c53eac3c930b2ae394 to your computer and use it in GitHub Desktop.
Save kardeiz/21c53eac3c930b2ae394 to your computer and use it in GitHub Desktop.
extern crate glob;
extern crate regex;
extern crate csv;
use std::vec::Vec;
use std::env;
use glob::glob;
use std::path::{Path, PathBuf};
use std::io::prelude::*;
use std::fs::File;
use regex::Regex;
use csv::Writer;
fn main() {
let sreg = Regex::new(r"(?i)section").unwrap();
let seid = Regex::new(r"\d{9}").unwrap();
let baid = Regex::new(r"\d{13}").unwrap();
let path = env::current_exe().unwrap();
let base = path.parent().unwrap();
env::set_current_dir(base).ok();
let txts: Vec<PathBuf> = glob("*.txt").unwrap().map(|r| r.unwrap()).collect();
let csv_out = Path::new("output.csv");
let mut writer = Writer::from_file(&csv_out).unwrap();
writer.encode( ("Items", "Section") );
for txt in txts {
let mut f = File::open(txt.as_path()).unwrap();
let mut s = String::new();
f.read_to_string(&mut s);
let mut csid = "";
let mut lines = s.lines_any().collect::<Vec<&str>>().into_iter().peekable();
loop {
if lines.peek().is_none() { break; }
let line = lines.next().unwrap().trim();
if sreg.is_match(line) {
loop {
if lines.peek().is_none() { break; }
if baid.is_match( lines.peek().unwrap() ) { break; }
let line = lines.next().unwrap().trim();
if seid.is_match(line) {
csid = line;
break;
} else {
continue;
}
}
} else if baid.is_match(line) {
let item = format!("=\"{}\"", line);
let sect = format!("=\"{}\"", csid);
writer.encode( (item, sect) );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment