I love this script so much! I must use this at least an average of once a day! The basic idea is that you tell it the positional columns you want to print and it prints them out.
columns [-h] [-s DELIM | -r REGEXP | -c] [-n] [-f FILENAME] [-v] column [column ...]
Option | Description | Default |
---|---|---|
-s DELIM or -F DELIM or --separator DELIM or --delim DELIM | Specifies the regular expression to separate columns. | The default is to separate columns by one or more whitespace characters |
-r REGEXP or --regexp REGEXP | Specifies a regular expression by which to break up tokens | The default is to separate columns by one or more whitespace characters |
-c or --csv | Reads input as CSV | The default is to separate columns by one or more whitespace characters |
-n or --negate | Excludes specified columns | Includes specified columns |
-f FILENAME or --filename FILENAME | Reads input from the specified file | The default is to read from stdin |
-v or --verbose | Enables debugging messages | Debugging messages are not printed |
You can can specify columns in a few ways:
- a single integer, the first column is 1
- two integers separated by a hyphen - 1-3 is synonymous with 1 2 3
- a single negative integer counts columns backwards - -1 is the last column
Be aware that if the first column you specify is negative, you'll likely have to use a trick to make it so that the script will not think that -1 is an option:
$ columns -- -1
This is a common technique when invoking Unix commands so it's handy to remember!
$ ls -l total 0 -rw-rw-r-- 1 bruno bruno 0 Aug 14 12:44 1 -r--r--r-- 1 bruno bruno 0 Aug 14 12:44 2 -rw-rw-r-- 1 bruno bruno 0 Aug 14 12:44 3 -rwxrwxr-x 1 bruno bruno 0 Aug 14 12:44 foobar.txt $ ls -l | columns 1 -1 total 0 -rw-rw-r-- 1 -r--r--r-- 2 -rw-rw-r-- 3 -rwxrwxr-x foobar.txt $
- The data is read from stdin. If stdin is not directed from a file or pipe, en error message is printed and the script terminates
- Negative columns don't work when `--negate` is specified and no error is thrown.