Skip to content

Instantly share code, notes, and snippets.

@nsonnad
Created July 28, 2014 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nsonnad/e6c0f974a1c6ba8413f6 to your computer and use it in GitHub Desktop.
Save nsonnad/e6c0f974a1c6ba8413f6 to your computer and use it in GitHub Desktop.
var fs = require('fs');
var stream = require('stream');
var byLine = new stream.Transform({objectMode: true});
var inputFile = fs.createReadStream('uspop.csv',{'encoding': 'utf8'});
var even = fs.createWriteStream('evenYears.csv');
var odd = fs.createWriteStream('oddYears.csv');
function evenOddYears (line) {
// check if year is even or odd
var currYear = line.slice(0, 4);
if (parseInt(currYear) % 2 === 0) {
console.log('even:', currYear);
even.write(new Buffer(line + '\n'));
} else {
console.log('odd:', currYear);
odd.write(new Buffer(line + '\n'));
}
}
byLine._transform = function (chunk, encoding, cb) {
var data = chunk.toString();
var lines = data.split('\n');
lines.forEach(evenOddYears);
cb();
};
inputFile.pipe(byLine).pipe(process.stdout);
process.stdout.on('error', process.exit);
year pop
2013 316.16
2012 313.91
2011 311.59
2010 309.35
2009 306.77
2008 304.09
2007 301.23
2006 298.38
2005 295.52
2004 292.81
2003 290.11
2002 287.63
2001 284.97
2000 282.16
1999 279.04
1998 275.85
1997 272.65
1996 269.39
1995 266.28
1994 263.13
1993 259.92
1992 256.51
1991 252.98
1990 249.62
1989 246.82
1988 244.5
1987 242.29
1986 240.13
1985 237.92
1984 235.83
1983 233.79
1982 231.66
1981 229.47
1980 227.23
1979 225.06
1978 222.59
1977 220.24
1976 218.04
1975 215.97
1974 213.85
1973 211.91
1972 209.9
1971 207.66
1970 205.05
1969 202.68
1968 200.71
1967 198.71
1966 196.56
1965 194.3
1964 191.89
1963 189.24
1962 186.54
1961 183.69
1960 180.67
1959 177.83
1958 174.88
1957 171.98
1956 168.9
1955 165.93
1954 163.03
1953 160.18
1952 157.55
1951 154.88
1950 152.27
1949 149.19
1948 146.63
1947 144.13
1946 141.39
1945 139.93
1944 138.4
1943 136.74
1942 134.86
1941 133.4
1940 132.12
1939 130.88
1938 129.83
1937 128.83
1936 128.05
1935 127.25
1934 126.37
1933 125.58
1932 124.84
1931 124.04
1930 123.08
1929 121.77
1928 120.51
1927 119.04
1926 117.4
1925 115.83
1924 114.11
1923 111.95
1922 110.05
1921 108.54
1920 106.46
1919 104.51
1918 103.21
1917 103.27
1916 101.96
1915 100.55
1914 99.11
1913 97.23
1912 95.34
1911 93.86
1910 92.41
1909 90.49
1908 88.71
1907 87.01
1906 85.45
1905 83.82
1904 82.17
1903 80.63
1902 79.16
1901 77.58
1900 76.09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment