Skip to content

Latest commit

 

History

History
72 lines (67 loc) · 2.59 KB

columns.md

File metadata and controls

72 lines (67 loc) · 2.59 KB

columns

Purpose

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.

Syntax

columns [-h] [-s DELIM | -r REGEXP | -c] [-n] [-f FILENAME] [-v] column [column ...]

Options and arguments

OptionDescriptionDefault
-s DELIM or -F DELIM or --separator DELIM or --delim DELIMSpecifies the regular expression to separate columns.The default is to separate columns by one or more whitespace characters
-r REGEXP or --regexp REGEXPSpecifies a regular expression by which to break up tokensThe default is to separate columns by one or more whitespace characters
-c or --csvReads input as CSVThe default is to separate columns by one or more whitespace characters
-n or --negateExcludes specified columnsIncludes specified columns
-f FILENAME or --filename FILENAMEReads input from the specified fileThe default is to read from stdin
-v or --verboseEnables debugging messagesDebugging 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!

Example

$ 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
$

Notes

  • 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.