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

more robust installation procedure for zsh and bash

parent 1c9255d0
......@@ -29,13 +29,13 @@ As an alternative to the automatic procedure:
```
wget https://projects.sisrlab.com/idm/csv-pipes/raw/master/csv-pipes.sh -O $HOME/.csv-pipes.sh
echo 'source $HOME/.csv-pipes.sh' >> $HOME/.bashrc
echo 'source $HOME/.csv-pipes.sh' >> $HOME/.bashrc # or .zshrc
```
What those two lines accomplish:
1. download the file `csv-pipes.sh` ([here](https://projects.sisrlab.com/idm/csv-pipes/raw/master/csv-pipes.sh)) and place it in your home directory
2. Add `source $HOME/csv-pipes.sh` at the end of your `.bashrc` or `.zshrc` file.
2. Add `source $HOME/.csv-pipes.sh` at the end of your `.bashrc` or `.zshrc` file.
## Usage
......
......@@ -3,16 +3,14 @@
# 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-pipes.sh"
CSV_URL='https://projects.sisrlab.com/idm/csv-pipes/raw/master/csv-pipes.sh'
# collapse fields containing newlines
csv.collapse() {
perl -pe "s/\\\\\n/ /" -
}
# remove all quotes from a CSV
# remove all quotes from a CSV
csv.unquote() {
sed "s/\"//g" -
}
......@@ -57,7 +55,7 @@ csv.read() {
if [[ -z `file $1 |grep gzip` ]];
then cat $1 | csv.collapse;
else zcat $1 | csv.collapse;
fi
fi
}
# For a CSV with a header row, print column names, each on a separate line
......@@ -65,16 +63,34 @@ csv.names() {
csv.read $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() {
csv.update
if [[ -z `grep csv-pipes.sh $PROFILE` ]];
then echo "source $CSV_SRC" >> $PROFILE;
# Check for bash
if [[ ! -z `echo $SHELL | grep bash` ]]; then
echo "Shell is bash"
CSV_PROFILE="$HOME/.bashrc";
fi
# Check for zsh
if [[ ! -z `echo $SHELL | grep zsh` ]]; then
echo "Shell is zsh"
CSV_PROFILE="$HOME/.zshrc";
fi
# Download CSV Pipes, if necessary
if [[ -z `ls $CSV_SRC` ]];
then wget -O $CSV_SRC 'https://projects.sisrlab.com/idm/csv-pipes/raw/master/csv-pipes.sh';
else echo "csv-pipes.sh is already downloaded.";
fi
# Add to shell profile, if necessary
if [[ -z `grep csv-pipes.sh $CSV_PROFILE` ]];
then
echo "Installing to $CSV_PROFILE";
echo "source $CSV_SRC" >> $CSV_PROFILE;
echo "CSV Pipes is installed to $CSV_SRC.";
else
echo "CSV Pipes is already installed to $CSV_SRC."
fi
}
......@@ -85,7 +101,7 @@ csv.ls() {
# check whether csv.[whatever] exists
_csv_function_exists() {
[[ ! -z `which csv.$1 | grep -v "not found"` ]]
[[ ! -z `typeset -f | grep "csv.$1"` ]]
}
# how many seconds does it take to count the lines in a file?
......@@ -95,7 +111,10 @@ csv.benchmark() {
# generic CSV runner
csv() {
_csv_function_exists $1 && csv.$1 "${@:2}"
if [[ ! -z `typeset -f | grep "csv.$1 \(\)"` ]];
then csv.$1 "${@:2}";
else echo "CSV Pipes command '$1' not found";
fi
}
# obtain all the commands and their short descriptions
......@@ -157,7 +176,7 @@ _csv_completion()
return 0
;;
esac
COMPREPLY=( $(compgen -W "$(_csv_commands_short)" -- ${cur}) )
return 0
}
......
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