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

The Geek Diary

CONCEPTS | BASICS | HOWTO

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • Linux Services
    • VCS
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
    • Data Guard
  • DevOps
    • Docker
    • Shell Scripting
  • Interview Questions
  • 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. NFSv4 Client Shows “nobody” As Owner And Group For Mount Point (CentOS/RHEL)
  2. Extend volume on non-partitioned disk (XFS) under VMware guest
  3. UNIX/Linux : Access control lists (ACLs) basics
  4. Linux OS Service ‘syslog’
  5. UNIX / Linux : Examples of bash history command to repeat last commands
  6. CentOS / RHEL 5 : How to disable device mapper multipath (dm-multipath)
  7. How to Setup a squid proxy server on CentOS/RHEL 7
  8. How To Retain Current And Older Linux Packages While Doing Update With ‘yum’ Command
  9. How to use wget to download file via proxy
  10. CentOS/RHEL – vgs command reports error: “global/global_filter” unknown

You May Also Like

Primary Sidebar

Recent Posts

  • Oracle Database – Configuring Secure Application Roles
  • Extend rule sets by using factors in Oracle Database Vault
  • What are Command Rules in oracle Database
  • Using Rule Sets in Oracle Database Vault
  • Archives
  • Contact Us
  • Copyright

© 2021 · The Geek Diary