Commit 247187da authored by Ian Dennis Miller's avatar Ian Dennis Miller

Update csv-functions.sh

parent 48893ffc
# Streaming CSV Functions
# 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.
......@@ -17,31 +17,62 @@ alias csv.unquote='sed "s/\"//g"'
alias csv.split='sed -e "s/,/\\n/g"'
# produce awk expression suitable for selecting the requested columns
csv.awk_select_column() { awk 'BEGIN {for (i=1; i < ARGC; i++) printf "$" ARGV[i] ", "}' $@ | rev | cut -c 3- | rev }
_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 `csv.awk_select_column $@` }" }
csv.select_column() {
awk -F, "BEGIN {OFS = \",\"} { print `_awk_select_column $@` }"
}
# limit number of rows returned
csv.limit() { head -n $1 }
csv.limit() {
head -n $1
}
# calculate sum of a column
csv.sum() { awk '{ s += $1 } END { printf "%.8f\n", s }' - }
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 }' - }
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 }' }
csv.count() {
wc -l - | awk '{ print $1 }'
}
# cat a CSV file
csv.cat() { cat $1 | csv.collapse }
# 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 }
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 }
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 }
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