• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

Using cut on Linux Terminal

by admin

The cut command

The cut command is most often used to select single columns of data from input separated by a single character, such as an /etc/passwd file. For example, the cut command is used to extract specified columns/characters of a piece of text, which is given as follows:

  • -c: Specifies the filtering of characters
  • -d: Specifies the delimiter for fields
  • -f: Specifies the field number

Cut Command Examples

The following are a few examples that show the usage of the cut command:

Example 1

Let’s start with a simple example of extracting a specific column from the /etc/passwd file. As the /etc/passwd file fields are delimited with “: (colon)” delimiter, we will use the option “-d:” in the command.

# cut -d: -f6 /etc/passwd

cut command examples in linux

In this example, -d specifies the delimiter or separator variable, in this case a colon, and -f specifies the number of the field (or column), starting from 1.

Example 2

We can also filter out multiple columns from the /etc/passwd file using the comma separated indices. For example:

# cut -d: -f1,3 /etc/passwd

using cut on linux terminal

The display will contain the login name and user ID.

Example 3

We can also specify the field numbers with hyphen-separated ranges. We can also combine the comma-separated indices and hyphen-separated ranges for filtering out the columns. For example:

# cut -d: -f1,3-4 /etc/passwd

cut command hyphen separated range and comma separated indices

Example 4

We can leave one of the numbers out of a range, to mean “up to” or “from”. For example, to filter out columns “upto 2”, use the below command:

# cut -d: -f-2 /etc/passwd

cut command in linux

Similarly, to display fields from 6 till the end, use the below command:

# cut -d: -f6- /etc/passwd

cut command for Linux Terminal

Example 5

However, cut is not limited to delimited data. It can also split on character counts with -c, or bytes with -b. This can be a useful way to get only a certain number or range of bytes per line. As shown in the example below, the output of the date command is sent as an input to the cut command and only the first three characters are printed on screen, which is shown as follows:

# date | cut -c1-3
Wed

The date command without the cut command, would print an output as shown below:

# date
Wed Dec  5 15:24:12 UTC 2018

Filed Under: Linux

Some more articles you might also be interested in …

  1. flock Command Examples in Linux
  2. Sample /etc/services file in Linux
  3. atop Command Examples in Linux
  4. pluma Command Examples in Linux
  5. UNIX / Linux : How to lock or disable an user account
  6. CentOS / RHEL : How to change password hashing algorithm
  7. 12 RPM (Red Hat Package Manager) Command Examples
  8. xz: command not found
  9. ORA-01031: insufficient privileges when creating a materialized view
  10. esearch Command Examples in Linux

You May Also Like

Primary Sidebar

Recent Posts

  • powertop Command Examples in Linux
  • powertop: command not found
  • powerstat: command not found
  • powerstat Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright