Shell Corner: Introducing rcutShell Corner: Introducing rcut
The cut command is one of the "arrows in the shell programmer's "quiver. This month, Anton Gorshkov presents bash shell script rcut that performs an anti-cut operation. Rather than including columns, rcut excludes them.
November 1, 2004
November 2004
Shell Corner: Introducing rcut
Hosted by Ed Schaefer
The cut command is one of the arrows' in the shell programmers quiver'. This month, Anton Gorshkov presents bash shell script rcut that performs an anti-cut operation. Rather than including columns, rcut excludes them.
reverse cut
Often when we deal with delimited files, we use the Unix cut command in order to output only certain columns, but more often we use it to exclude certain columns. For example, to exclude columns 4,5, and 10 from a pipe-delimited file, we would use:
cut -d"|" -f1-3,6-9,11- file.txt
This is a bit convoluted and it's not clear by looking at the command what is it doing. Introducing rcut (reverse cut). This little script does exactly the opposite of cut. Instead of specifying which columns to include, you specify the columns you want to exclude. Thus, to accomplish the same thing as shown in the previous example, you would use: rcut -d "|" -f4,5,10 file.txt which is a lot cleaner and easier to use. Here is the bash script:
#!/bin/bash
#=================================================================#
# Program : rcut
# Description : Same as "cut" except you specify the columns you want to EXCLUDE
# Author : Anton Gorshkov
# Created : 20040304
# Last Update : 20040315 - Made Sorting Cleaner.
# Usage : rcut -d<delim> -f<columns to exlude> <file> (can be piped to as well)
#================================================================#
usage="Usage: rcut -d<delim> -f<column to exlude> <file>"
while getopts ":d:f:" opt; do
case $opt in
d ) delim=$OPTARG ;;
f ) excludeColumns=$OPTARG ;;
\? ) echo $usage
exit 1 ;;
esac
done
shift $(($OPTIND -1))
if [ -z $delim ] || [ -z $excludeColumns ]; then
echo $usage
exit 1
fi
#This is to sort the columns as they need to be in order.
names=(`echo $excludeColumns | tr ',' '\012' | sort -n | xargs`)
myStr=""
declare -i last=0
declare -i from=0
declare -i to=0
for i in "${names[@]}";do
from=$last+1
to=$i-1
last=$i;
if [ $i -gt $from ]; then
myStr=$myStr$from-$to,
fi
done
last=$last+1
myStr=$myStr$last-
cut -d"$delim" -f$myStr $1
A Korn shell script will look very similar:
#! /bin/ksh -f
usage="Usage: rcut -d<delim> -f<column to exlude> <file>"
while getopts ":d:f:" opt; do
case $opt in
d ) delim=$OPTARG ;;
f ) excludeColumns=$OPTARG ;;
\? ) echo $usage
exit 1 ;;
esac
done
shift $(($OPTIND -1))
if [ -z $delim ] || [ -z $excludeColumns ]; then
echo $usage
exit 1
fi
#Don't have to sort, KornShell does it for you!
set -sA names `echo $excludeColumns | sed -e 's/,/ /g'`
myStr=""
last=0
from=0
to=0
for i in "${names[@]}";do
((from=$last+1))
((to=$i-1))
((last=$i))
if [ $i -gt $from ]; then
myStr=$myStr$from-$to,
fi
done
((last=$last+1))
myStr=$myStr$last-
cut -d${delim} -f$myStr $1
Besides the difference in mathematical expressions, the big advantage of Korn shell in this case is that it allows you to declare a sorted array. Not that its a big deal to sort, I just find that Korn shell is a lot cleaner in this case. One practical application for rcut is if you want to compare two files and ignore certain columns. In fact, I wrote rcut just for that purpose. Imagine you have two files that contain the same contact information: File1:
Id |Fname|Lname |Month |Day |UpdateDate
1 |Anton|Gorshkov |May |20 |03/18/2004
2 |John |Doe |Dec |19 |02/01/2004
3 |Ed |Schaefer |March |11 |05/29/2003
File2:
Id |Fname|Lname |Month |Day |UpdateDate
1 |John |Doe |Dec |19 |08/31/2002
2 |Ed |Schaefer |March |11 |09/09/2001
3 |Anton|Gorshkoff |May |20 |05/28/2002
You want to find out which contacts are out of sync, but you dont care about the Id' and the UpdateDate' field. I wrote another little script called diffi (diff ignore) that compares two files, sorting them prior to comparison and ignores specified columns even if they are different:
#!/bin/bash
#==================================================================#
# Program : diffi
# Description : diff that allows you to exclude columns. (requires rcut)
# Author : Anton Gorshkov
# Created : 20040319
# Last Update : 20040319 - Initial Implementation
# Usage : diffi -d<delim> -f<columns to exlude> <file1> <file2>
#=================================================================#
usage="Usage: diffi -d<delim> -f<columns to exlude> <file1> <file2>"
while getopts ":d:f:" opt; do
case $opt in
d ) delim=$OPTARG ;;
f ) excludeColumns=$OPTARG ;;
\? ) echo $usage
exit 1 ;;
esac
done
shift $(($OPTIND -1))
if [ -z $delim ] || [ -z $excludeColumns ]; then
echo $usage
exit 1
fi
rcut -d$delim -f$excludeColumns $1 | sort > $1.cut
rcut -d$delim -f$excludeColumns $2 | sort > $2.cut
diff $1.cut $2.cut
rm $1.cut $2.cut
Usage: diffi d'|' f1,6 file1 file2
This will show you that the only different contact is me. (LName is misspelled). Notice that this script is not perfect' in a sense that it doesnt support any standard diff options, but that could be incorporated.
Anton Gorshkov graduated from Polytechnic University with a Bachelor's Degree in Computer Science. He now works for one of the leading Wall Street Investment Banks, developing Data Warehouse Infrastructure for FICC & Equities trading systems. He can be reached at: [email protected].