diff options
Diffstat (limited to 'bin/grep.js')
| -rw-r--r-- | bin/grep.js | 47 |
1 files changed, 32 insertions, 15 deletions
diff --git a/bin/grep.js b/bin/grep.js index af30bf2..b4eb3bd 100644 --- a/bin/grep.js +++ b/bin/grep.js @@ -60,7 +60,6 @@ const fs = require('fs') const parse = require('csv-parse') const stringify = require('csv-stringify') -const search = "AR-15" const fields = ("incident_id,date,state,city_or_county," + "address,n_killed,n_injured,incident_url,source_url," + "incident_url_fields_missing,congressional_district," @@ -73,13 +72,21 @@ const fields = ("incident_id,date,state,city_or_county," .split(',') .reduce((a,b,i) => { a[b] = i + return a }, {}) -const search = 'AR-15' +const search = 'good-samaritans' const regexp = new RegExp(search, 'i') function test(row){ - return row[fields.gun_type].match(regexp) + return ( + row + // && row[fields.incident_characteristics].indexOf('Non-Shooting Incident') === -1 + && row[fields.incident_characteristics].indexOf('Samaritan') !== -1 + // // && row[fields.participant_age_group].indexOf('Teen') !== -1 + // && row[fields.participant_age_group].indexOf('Child') !== -1 + // && row[fields.gun_type].indexOf('AR-15') !== -1 + ) } const input = fs.createReadStream('./data/gun_violence.csv') @@ -87,39 +94,49 @@ const parser = parse() const stringifier = stringify() const output = fs.createWriteStream('./data/' + search + '.csv') -stream.on('readable', function() { - var buf; - while ((buf = stream.read()) !== null) { +input.on('readable', function() { + let buf + while ((buf = input.read()) !== null) { parser.write(buf) } -}); -stream.on('finish', function(){ +}) +input.on('error', function(err){ + console.error('input error', err.message) +}) +input.on('finish', function(){ parser.end() }) +let i = 0 parser.on('readable', function(){ - let record - while (record = parser.read()) { - if (test(record[fields])) { - stringifier.write(record) + let row + while (row = parser.read()) { + if (i === 0) { + stringifier.write(row) + } + if ((++i % 10000) === 0) { + console.log(i + '...') + } + if (test(row)) { + stringifier.write(row) } } }) parser.on('error', function(err){ - console.error(err.message) + console.error('parser error', err.message) }) parser.on('end', function(){ stringifier.end() }) stringifier.on('readable', function(){ - let row; + let row while(row = stringifier.read()){ output.write(row) } }) stringifier.on('error', function(err){ - // console.error(err.message) + console.error('stringifier error', err.message) }) stringifier.on('finish', function(){ output.end() |
