View
@@ -0,0 +1,34 @@
#!/bin/bash
#
# Usage:
# ./csv-concat-test.sh <function name>
set -o nounset
set -o pipefail
set -o errexit
test-good() {
mkdir -p _tmp
cat >_tmp/test1.csv <<EOF
name,age
alice,0
bob,10
EOF
cat >_tmp/test2.csv <<EOF
name,age
carol,20
EOF
./csv_concat.py _tmp/test{1,2}.csv
cat >_tmp/bad.csv <<EOF
name,age,another
dave,30,oops
EOF
./csv_concat.py _tmp/test{1,2}.csv _tmp/bad.csv
}
"$@"
View
@@ -0,0 +1,35 @@
#!/usr/bin/python
"""
csv_concat.py
"""
import sys
def main(argv):
first_header = None
for path in argv[1:]:
with open(path) as f:
# Assume
header = f.readline()
if first_header is None:
sys.stdout.write(header)
first_header = header.strip()
else:
h = header.strip()
if h != first_header:
raise RuntimeError(
'Invalid header in %r: %r. Expected %r' % (path, h,
first_header))
# Now print rest of lines
for line in f:
sys.stdout.write(line)
if __name__ == '__main__':
try:
main(sys.argv)
except RuntimeError as e:
print >>sys.stderr, 'FATAL: %s' % e
sys.exit(1)