home *** CD-ROM | disk | FTP | other *** search
- #!/bin/sh
- # Shell/awk version of System V cut(1) command
- # Is not fully compatible, nor optimized (-c option is SLOW).
- # Incompatibilities include
- # - Different diagnostics
- # - Arbitrary limits on number of columns/fields it will output
- # - Does not process delimiter-less records properly
- # - Does not implement the -s option
-
- # Written by Jon LaBadie (princeton!jonlab!jon) in July, 1990 while
- # teaching a UNIX Tools class on a Xenix system that did not include
- # cut. The command was needed for some of the class exercises.
-
- # In no way would I recommend it be used as a replacement for cut(1).
- # But if you need the capabilities of cut, this might do.
-
- # set -u
-
- PROG=${0}
-
- usage() {
- # usage message should be revised to include -d option
- echo "Usage: ${PROG} -{f|c}spec [file(s)]" >&2
- echo " where: spec is m,n or m-n, or n-" >&2
- exit 1
- }
-
- COLUMNS=FALSE
- FIELDS=FALSE
- DELIM=FALSE
-
- while [ ${#} -gt 0 ]
- do
- case "${1}"
- in
- -f*) [ ${COLUMNS} != FALSE ] && usage
- FIELDS=`expr "${1}" : '..\(.*\)'`
- [ "${FIELDS}" = "" ] && {
- echo "${PROG}: -f option requires an argument" >&2
- usage
- }
- ;;
- -c*) [ ${FIELDS} != FALSE ] && usage
- COLUMNS=`expr "${1}" : '..\(.*\)'`
- [ "${COLUMNS}" = "" ] && {
- echo "${PROG}: -c option requires an argument" >&2
- usage
- }
- ;;
- -d) echo "${PROG}: -d option requires a single character argument" >&2
- usage
- ;;
- -d?) DELIM=`expr "${1}" : '..\(.*\)'`
- ;;
- -d??*) echo "${PROG}: single character delimiters only" >&2
- exit 1
- ;;
- -?*) echo "${PROG}: Illegal Option: \"${1}\"" >&2
- usage
- ;;
- *) break # start of file names
- ;;
- esac
- shift
- done
-
- [ "${FIELDS}" = FALSE -a "${COLUMNS}" = FALSE ] && {
- echo "${PROG}: either -f or -c option must be used" >&2
- usage
- }
-
- [ "${DELIM}" != FALSE -a "${COLUMNS}" != FALSE ] && {
- echo "${PROG}: -d and -c options are incompatible" >&2
- usage
- }
-
- [ "${DELIM}" = FALSE ] && DELIM=' ' # TAB character
-
- FILES=${@+"${@}"}
-
- if [ "${FIELDS}" != FALSE ]
- then
- awk -F"${DELIM}" '
- BEGIN {
- OFS = "'"${DELIM}"'"
- stderr = "/dev/tty"
-
- n = split("'"${FIELDS}"'", tmp1, ",")
- j = 0
-
- for (i = 1; i <= n; i++) {
- if ( (pos = index(tmp1[i], "-")) == 0) {
- # must be simple n type column spec
- cols[++j] = tmp1[i] + 0;
- }
- else if (pos == length(tmp1[i])) {
- # must be n- type column specifier
- if (i != n) {
- print "'"${PROG}"'" ": Field specifier " tmp1[i] ", must be last" > stderr
- exit 1
- }
- v = substr(tmp1[i], 1, pos - 1) + 0
- while (j < 48) # arbitrary max of 48
- cols[++j] = v++
- }
- else {
- # last possibility is m-n type
- n2 = split(tmp1[i], tmp2, "-")
- while (tmp2[1] <= tmp2[2])
- cols[++j] = tmp2[1]++ + 0
- }
- }
- ncols = j
- for (i = 1; i < ncols; i++)
- if (cols[i] >= cols[i+1]) {
- print "'"${PROG}"'" ": Fields must be specified in ascending order" >stderr
- exit 1
- }
- }
-
- {
- printf("%s", $(cols[1]))
- for (i = 2; i <= ncols && cols[i] <= NF; i++) {
- printf("%s%s", OFS, $(cols[i]))
- }
- print ""
- }
- ' ${FILES}
- else
- # must be -c option
- awk '
- BEGIN {
- stderr = "/dev/tty"
-
- n = split("'"${COLUMNS}"'", tmp1, ",")
- j = 0
-
- for (i = 1; i <= n; i++) {
- if ( (pos = index(tmp1[i], "-")) == 0) {
- # must be simple n type column spec
- cols[++j] = tmp1[i] + 0;
- }
- else if (pos == length(tmp1[i])) {
- # must be n- type column specifier
- if (i != n) {
- print "'"${PROG}"'" ": Column specifier " tmp1[i] ", must be last" > stderr
- exit 1
- }
- v = substr(tmp1[i], 1, pos - 1) + 0
- while (j < 200) # arbitrary max of 200
- cols[++j] = v++
- }
- else {
- # last possibility is m-n type
- n2 = split(tmp1[i], tmp2, "-")
- while (tmp2[1] <= tmp2[2])
- cols[++j] = tmp2[1]++ + 0
- }
- }
- ncols = j
- for (i = 1; i < ncols; i++)
- if (cols[i] >= cols[i+1]) {
- print "'"${PROG}"'" ": Columns must be specified in ascending order" >stderr
- exit 1
- }
- }
-
- {
- printf("%s", substr($0, cols[1], 1))
- l = length
- for (i = 2; i <= ncols && cols[i] <= l; i++) {
- printf("%s", substr($0, cols[i], 1))
- }
- print ""
- }
- ' ${FILES}
- fi
-
- exit 0
-
-
-