#!/bin/bash #extract columns from delimited files #ex_dlm -c col1,..,coln [-d DFS] {-h} file1.tsv file2.tsv... | pipe #output: columns col1,.., coln # -c list of column numbers, comma separated, no space in between # -d input field separator, ["\t"] # [] default, {} optional, all others required #BUG: Do not set DFS="\t" on the command line. It is the default, in #any case. #----------------------------------------------------------------------- DFS=" "; #this is "cntrlV and tab" HELP=; #----------------------------------------------------------------------- while getopts c:d:h OPTVAL do case $OPTVAL in c) reqcol=$OPTARG;; d) DFS=$OPTARG;; h) HELP=1;; *) echo "ex_dlm -h for help"; exit -1;; esac done shift $((OPTIND-1)) if [ $HELP ]; then echo "ex_dlm -c col1,col2,.. -d DFS file1.tsv file2.. | pipe" echo "-c list of column numbers, comma separated, no space" echo '-d input field separator, ["\t"]' exit fi [ -z $reqcol ] && { echo "must specify columns; exiting"; exit -1;} #----------------------------------------------------------------------- # check to see if data is being supplied by pipe #----------------------------------------------------------------------- if [ $# -eq 0 ]; then if [ -p /dev/stdin ]; then set -- "/dev/stdin" #set $1=/dev/stdin else echo "no file given nor is there a trailing pipe"; exit -1 fi fi for i in $* do cut -f$reqcol -d "$DFS" $i done