Cut command

From Linux Bash Shell Scripting Tutorial Wiki
Jump to navigation Jump to search

Use the cut command to cut and display selected parts of lines on Linux and Unix.

Purpose

We use the cut command to cuts the following from file or stdin and write them to another file or stdout:

  1. Bytes
  2. Characters
  3. Fields from each line of a file

In other words, use the cut command to cut out selected fields from each line of a file.

Syntax

The syntax is:

cut filename
cut [options] filename
command | cut 
command1 | command2 | cut > output

Examples

Let us consider the following os.txt file:

#OS_NAME|DESCRIPTION|Type|First release date#
CentOS|Community Enterprise Operating System|Linux|14 May 2004
RHEL|Red Hat Enterprise Linux|Linux|February 22, 2000
Debian|Debian GNU/Linux|Linux|September 1993
FreeBSD|N/A|BSD|1 November 1993
Alpine|N/A|Linux|August 2005
AIX|Advanced Interactive eXecutive|Unix|February 1986
macOS|Mac OS X or OS X|Unix|March 24, 2001

If you only wanted the OS name, use the cut command to get only that information:

cut -d'|' -f 1 os.txt

How about printing both the OS name and its description? Try

cut -d'|' -f 1,2 os.txt
Cut command in Linux with examples
Cut command in Linux with examples

The -d option state to use '|' character as the field delimiter. It would be best if you put quotation marks around characters with special meaning to the shell, such as the space character or '|'.The -f option defines a list of fields assumed to be separated in the file by a delimiter character, which is by default the tab character. However, we used the -d option to override it to pipe character (|). In other words, we used cut command to display several fields of each line of a file named os.txt.

Select parts of a line

The following command shows the Linux login name and complete user name fields of the system password file. These are the first and fifth fields separated by colons:

cut -d ':' -f 1,5 /etc/passwd

# filter out user name 'vivek' using the grep command #
cut -d ':' -f 1,5 /etc/passwd | grep vivek

Sample outputs from last command:

vivek:Vivek Gite

Specifying character positions

Consider the following lines of input from log.txt file:

22/Jul/2020:05:27:11 +0000 "POST /wiki/api.php HTTP/1.1" 200 87
23/Aug/2021:05:27:27 +0000 "POST /wiki/api.php HTTP/1.1" 499 0
01/Sep/2021:05:27:27 +0000 "POST /wiki/index.php?title=Cut_command&action=submit HTTP/1.1" 302 5
13/Oct/2020:05:27:27 +0000 "GET /guide/Cut_command HTTP/1.1" 200 6554 
22/Jul/2021:05:30:47 +0000 "GET /wiki/index.php?title=Cut_command&action=edit&section=4 HTTP/1.1" 200 6

If we wanted to extract the three characters giving the month, these would be the characters at positions 4, 5, and 6. Please note that column numbering starts at 1 when using the cut tool. So we could give the following cut command to extract those three characters from each line:

# get month
cut -c 4-6 log.txt

# get year
cut -c 8-11 log.txt 

# get day
cut -c 1-2 log.txt

The -c range or -c list command is used to specify a list or range. For instance, option -c 1-40, the cut command writes out the first 40 characters in each line of the file.

Specifying byte positions

The syntax is as follows:

cut -b range file
cut -b list file
cut -b 1-3 filename

The -c and -b options are almost the same. However, internationalization will change that with -c option.

Using cut with other commands

The who command shows information about users who are currently logged in, including their IP address (or hostname). For example:

who

Outputs:

vivek            pts/0        2021-07-22 05:10 (192.168.2.25)
ubuntu           pts/0        2021-07-22 05:13 (172.xxx.yyy.zzz)

Let us show the names and login times of the currently logged in users:

who | cut -c 1-8,33-39

Here is what I see:

vivek    05:10
ubuntu   05:13

Here is how to list the Saturday dates for the current month:

cal
cal | cut -c 19-21

Sample outputs:

Sa 
 3 
10 
17 
 23
31

Exit status

This command returns the following exit values or status:

An exit status of cut command
Value Description
Zero (0) Success, i.e., all input files were output successfully.
Non-zero (>0) An error/failure occurred.

Here is how to use the exit status using if command in our script to find out if the cut command was successful or not

#!/bin/bash
# some logic

# run cut to see if we can grab VLAN hostnames else die 
if ! cut -d ':' -f 1,16 ~/.cluster.conf >/dev/null
then
	echo "VLAN host name not found in ~/.cluster.conf"
    exit 1
fi

# rest of the script continue of no error occurred with the the cut command
rsync ....

Getting help

Type the man command or help command as follows:

man cut
cut --help
GNU/Linux cut options
Option Description
-b LIST Select only these bytes.
-c LIST Select only these characters.
-d 'DELIM' Use DELIM instead of TAB for field delimiter.
-f LIST Select only these fields; also print any line that contains no delimiter character, unless the -s option is specified.
--complement Complement the set of selected bytes, characters or fields
-s Do not print lines not containing delimiters.
--output-delimiter=STRING Use STRING as the output delimiter the default is to use the input delimiter.
-z Line delimiter is NUL, not newline.
--help Display this help and exit.
--version Output version information and exit.

See also