On Tue, 10 Apr 2012, Tony Yarusso wrote:

> It's stupidly simple, but lately I've been getting a lot of use for the 
> 'cut' command.


I use it all the time.  In fact, I try to use tab-delimiters as much as 
possible so that I can use cut and paste without any -d option.

Since we're on the topic of tricks we've learned -- see a couple of very 
simple one-liner bash scripts below that I use often.  The second script 
was invented for use with cut.

Mike


-------begin "header" script on next line----------------
#!/bin/bash

# Takes the header (first) line of a tab-delimited file and
# sends a list of the field names with field numbers to stdout.
#
# Usage:
# 
# header filename
# cut -f3- filename | header -

head -1 "$1" | tr '\t' '\n' | cat -n
------end "header" script on previous line---------------


-------begin "header2" script on next line----------------
#!/bin/bash

# Often used with my script called "header."  This reads in the output
# of header, often after processing by a grep command, and it produces
# a comma-delimited string of field numbers that can be used with the
# cut command.
#
# Normal usage:
#
# header filename | grep something | header2
#
# or, say,
#
# cut -f $(header filename | grep -i date | header2) filename
#

# Note: It is just taking the list of numbers in the first field of
# output from cat -n and replacing newlines with commas, then removing
# the final comma.  The result is a comma delimited list of numbers
# that does not end with a newline.  This is good for use with cut -f.

gawk '{print $1}' "$1" | tr -d ' ' | tr '\n' ',' | perl -pe 's/,\Z//'
------end "header2" script on previous line---------------