Cut
August 17, 2010
Unix V7 provided a utility called cut
that reads its input a line at a time and selectively copies portions of each input line to standard output. Portions are selected either by character position or by character-delimited field. Cut
is invoked as cut -c
list [file …] or cut -f
list [-d
char] [file …].
Character mode, invoked with the -c
option, retains in the output those character positions that are mentioned in the list, which may contain column numbers, or ranges of column numbers separated by a dash, all separated by commas; counting starts from one. Field mode, invoked with the -f
option, specifies a list of fields in a similar manner to character mode; fields are delimited by tab characters unless the field delimiter is changed by the -d
option.
For example, the command cut -f1,3 -d: /etc/passwd
prints user names and userid numbers from the password file.
Your task is to write a program to implement cut
. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
This was pretty fun to write.
I couldn’t help but have a try at it in Elisp. Of course I think it’s useless, but there it is:
Here it is in ruby …
This one prints the fields/columns in the order that they are given in in the command line unlike the Unix cut. I like it better this way but if you don’t then just sort the print_list on the return from parse_list(). The only other oddity is printing the separator in the field. Basically, if we decide to print a field and we’re past the first element, we’ll print the separator before the field. This makes it so we don’t have to have a separator hanging out there after the last field is printed.
Here is my complete implementation in c
http://codepad.org/QZL317EK