Commit d86fa346 authored by Ian Dennis Miller's avatar Ian Dennis Miller

initial commit

parent 42987d0d
# UNIX CSV Functions (csv-unix)
# Ian Dennis Miller
# Perform streaming operations with CSV files.
# When files are too large to fit into memory, streaming might be the only alternative.
PROFILE="$HOME/.profile"
CSV_SRC="$HOME/.csv-functions.sh"
CSV_URL='https://projects.sisrlab.com/idm/csv-unix/raw/master/csv-functions.sh?inline=false'
# collapse fields containing newlines
alias csv.collapse='perl -pe "s/\\\\\n/ /" -'
# remove all quotes from a CSV file
alias csv.unquote='sed "s/\"//g"'
# split a comma-delimited row onto separate lines
alias csv.split='sed -e "s/,/\\n/g"'
# produce awk expression suitable for selecting the requested columns
_awk_select_column() {
awk 'BEGIN {for (i=1; i < ARGC; i++) printf "$" ARGV[i] ", "}' $@ | rev | cut -c 3- | rev
}
# select a single column from a CSV by its index, starting at 1
csv.select_column() {
awk -F, "BEGIN {OFS = \",\"} { print `_awk_select_column $@` }"
}
# limit number of rows returned
csv.limit() {
head -n $1
}
# calculate sum of a column
csv.sum() {
awk '{ s += $1 } END { printf "%.8f\n", s }' -
}
# calculate mean of a column
csv.mean() {
awk '{ s += $1; n++ } END { printf "%.8f\n", s/n }' -
}
# count the number of lines in a CSV file
csv.count() {
wc -l - | awk '{ print $1 }'
}
# cat a CSV file, even if it is gzipped
csv.cat() {
if [[ -z `file $1 |grep gzip` ]];
then cat $1 | csv.collapse;
else zcat $1 | csv.collapse;
fi
}
# For a CSV with a header row, print column names, each on a separate line
csv.names() {
csv.cat $1 | csv.limit 1 | csv.unquote | csv.split
}
# built-in updater
csv.update() {
wget -O $CSV_SRC $CSV_URL; source $CSV_SRC
}
# make installation permanent
csv.install() {
if [[ -z `grep csv-functions.sh $PROFILE` ]];
then echo "source $CSV_SRC" >> $PROFILE;
fi
}
# print help
csv.help() {
echo "csv-unix 0.1\n\nAvailable commands:\n"
grep '^csv\.' ~/.csv-functions.sh | sed 's/alias //' | sed 's/=.*//' | sed 's/(.*//'
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment