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

The Geek Diary

HowTos | Basics | Concepts

  • Solaris
    • Solaris 11
    • SVM
    • ZFS
    • Zones
    • LDOMs
    • Hardware
  • Linux
    • CentOS/RHEL 7
    • RHCSA notes
    • SuSE Linux Enterprise
    • Linux Services
  • VCS
    • VxVM
  • Interview Questions
  • oracle
    • ASM
    • mysql
    • RAC
    • oracle 12c
    • Data Guard
  • DevOps
    • Docker
    • Shell Scripting
  • Hadoop
    • Hortonworks HDP
      • HDPCA
    • Cloudera
      • CCA 131

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. RPM command examples to query, install, remove and upgrade packages
  2. How to use strace and ltrace commands in Linux
  3. Beginners guide to Device Mapper (DM) multipathing
  4. “uname” Command Examples to Check UNIX/Linux Version
  5. Unmounting a Windows Share Fails in Linux
  6. Firewalld Command line Reference (Cheat Sheet)
  7. CentOS / RHEL 5 : How to use the faillog command to track failed login attempts
  8. How to recover from deleted root entry in /etc/shadow and/or /etc/passwd files in CentOS / RHEL 6
  9. List of SELinux Utilities
  10. RedHat / CentOS : Managing software RAID with mdadm

You May Also Like

Primary Sidebar

Recent Posts

  • How to Configure Network Namespaces in Docker Containers
  • How to change the default IP address of docker bridge
  • “su: Authentication failure” – in Docker
  • How to Pause and Resume Docker Containers
  • How to find docker storage device and its size (device mapper storage driver)
  • Archives
  • Contact Us
  • Copyright

© 2019 · The Geek Diary