How to Extract Columns of Data Using the Linux cut Command

When you dump data from a database or analyze a dense system log file in Linux, you are rarely interested in reading every single word on every single line. If you are looking at a massive CSV (Comma Separated Values) file containing thousands of users, you might only care about extracting the email addresses located in the third column, while completely ignoring the names and phone numbers. Instead of opening the file in a spreadsheet application or writing a complex Python script, you can use the native Linux cut command to slice the data vertically and extract exactly the columns you need.

Extracting Data by Character Position

The simplest way to use cut is to extract data based on its strict character position on a line, using the -c (character) flag. This is useful for parsing files that have a rigid, fixed-width format.

Suppose you have a file named inventory.txt where every product code is exactly 8 characters long, followed by a space, and then the product name.

  1. Open your Linux terminal.
  2. To extract only the 8-character product codes, type: cut -c 1-8 inventory.txt
  3. Press Enter.

The terminal will output a clean list of the product codes, completely slicing off the product names that followed them. You can also specify specific, non-contiguous characters using commas (e.g., cut -c 1,3,5).

Extracting Data by Delimiter and Field

In the real world, most data is not fixed-width. It is delimited by specific characters like commas, tabs, or colons. To parse this data, you must use the -d (delimiter) flag to tell cut what character separates the columns, and the -f (field) flag to tell it which column to extract.

The classic example is the Linux /etc/passwd file, which stores user account information separated by colons (:). The first column is the username.

  1. To extract only a list of usernames from the system, type: cut -d ':' -f 1 /etc/passwd
  2. Press Enter.

In this command:
-d ':' tells the system that the colon is the dividing wall between columns.
-f 1 tells the system to only grab the very first column (Field 1).

If you wanted to extract a list of usernames and their assigned home directories (which is the sixth column), you would combine the fields with a comma:
cut -d ':' -f 1,6 /etc/passwd

Piping Data into Cut

The cut command is most powerful when used as a filter in a pipeline, processing the output of other commands on the fly.

For example, if you run the ls -l command to view file permissions, it outputs a massive, messy table. If you only want a clean list of the file owners (which is the third column, separated by spaces), you can pipe the output directly into cut.

ls -l | tr -s ' ' | cut -d ' ' -f 3

(Note: Because ls -l uses an inconsistent number of spaces, the tr -s ' ' command is used in the middle of this pipeline to “squeeze” multiple spaces into a single space, ensuring cut reads the columns correctly).

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.