cut Command in Linux with Examples

The cut command in Linux is used to remove sections from each line of files and print specific parts of lines from each file to standard output.

Syntax:
The syntax for the cut command is as follows:

cut [OPTION]... [FILE]...
  • Options: Specifies various options, such as the delimiter to use and the fields to extract.
  • File: The input file to be processed.

Options:

  • -b, –bytes=LIST: Outputs only the specified bytes.
  • -c, –characters=LIST: Outputs only the specified characters.
  • -d, –delimiter=DELIM: Uses the specified delimiter instead of the tab character.
  • -f, –fields=LIST: Outputs only the specified fields; also print any line that contains no delimiter character, unless the -s option is specified
  • -n with -b: Does not split multibyte characters.
  • -s, –only-delimited: Does not print lines that do not contain delimiters.
  • –output-delimiter=STRING: Uses the specified string as the output delimiter.
  • –help: Displays the help text and exits.
  • –version: Displays the version information and exits.

Use only one of the -b, -c, or -f options. Each list can have one range, or multiple ranges separated by commas. Each range can be one of the following:

  • N: N’th byte, character, or field, counted from 1
  • N-: From N’th byte, character, or field, to the end of the line.
  • N-M: From N’th to M’th (inclusive) byte, character, or field.
  • -M: From the first to M’th (inclusive) byte, character, or field.

If no file is specified, or if the file is -, read standard input.

cut Command Examples in Linux:

advertisement
advertisement

In the majority of cut command examples in Linux, we’ll utilize the following test file:

$ cat test.txt
cat command for file oriented operations.
cp command for copy files or directories.
ls command to list out files and directories with its attributes.

Example 1: Select Column of Characters

To extract a specific column of characters from a file, use the -c option.

Note: Join free Sanfoundry classes at Telegram or Youtube
$ cut -c5 test.txt
c
o
o

Here, cut -c5 test.txt extracts the fifth character from each line in the test.txt file.

Example 2: Select Column of Characters using Range

To extract a range of characters from each line in a file, use the -c option with a range.

$ cut -c2-4 test.txt
at 
p c
s c

For example, the command cut -c2-4 test.txt will extract characters 2 through 4 from each line in the file test.txt.

advertisement

Example 3: Select Column of Characters using either Start or End Position

$ cut -c2- test.txt
at command for file oriented operations.
p command for copy files or directories.
s command to list out files and directories with its attributes.

This example extracts all characters from the second character to the end of each line in the test.txt file.

$ cut -c-2 test.txt
ca
cp
ls

This example extracts 2 characters from the beginning of each line from test.txt file.

advertisement

Example 4: Specifying delimiter

The following example displays only first field of each lines from /etc/passwd file using the field delimiter : (colon). In this case, the 1st field is the username.

$ cut -d':' -f1 /etc/passwd
root
daemon
bin
sys
sync
games
man
lp
mail
news
uucp
proxy
www-data
backup
list
irc
gnats
libuuid
syslog
messagebus
avahi-autoipd
usbmux
dnsmasq
whoopsie
kernoops
rtkit
speech-dispatcher
lightdm
avahi
colord
pulse
hplip
saned
abc
nobody

Example 5: Select Multiple Fields from a File

Below example displays username and home directory of users who has the login shell as “/bin/bash”.

$ grep "/bin/bash" /etc/passwd | cut -d':' -f1,6
root:/root
abc:/home/abc

Combining grep with cut allows extracting multiple fields (e.g., username and home directory) based on a specific condition (/bin/bash in this case) from the /etc/passwd file.

Example 6: Select All Fields Except the Specified Fields

The following example displays all the fields from /etc/passwd file except field 7

$ grep "/bin/bash" /etc/passwd | cut -d':' --complement -s -f7
root:x:0:0:root:/root
abc:x:1000:1000:abc,,,:/home/abc

Using –complement with -f suppresses the specified field (here, field 7) and displays all other fields from the /etc/passwd file based on the condition specified by grep.

Example 7: Change Output Delimiter for Display
To change the output delimiter use the option –output-delimiter as shown below. In this example, the input delimiter is : (colon), but the output delimiter is # (hash).

$ grep "/bin/bash" /etc/passwd | cut -d':'  -s -f1,6,7 --output-delimiter='#'
root#/root#/bin/bash
abc#/home/abc#/bin/bash

If output delimiter is new line, then output for a single user will be

$ grep abc /etc/passwd | cut -d':' -f1,6,7 --output-delimiter=$'\n'
abc
/home/abc
/bin/bash

Example 8: Select Fields Only When a Line Contains the Delimiter

In our /etc/passwd example, if you pass a different delimiter other than : (colon), cut will just display the whole line.

In the following example, we’ve specified the delimiter as | (pipe), and cut command simply displays the whole line, even when it doesn’t find any line that has | (pipe) as delimiter.

$ grep "/bin/bash" /etc/passwd | cut -d'|'  -f1
root:x:0:0:root:/root:/bin/bash
abc:x:1000:1000:abc,,,:/home/abc:/bin/bash

But, it is possible to filter and display only the lines that contains the specified delimiter using -s option.

The following example doesn’t display any output, as the cut command didn’t find any lines that has | (pipe) as delimiter in the /etc/passwd file.

$ grep "/bin/bash" /etc/passwd | cut -d'|' -s -f1

Example 9: Create a new file that contained just the username (field 1) and the user ID (UID)
This example extracts the first and third fields from the /etc/passwd file, then redirects (>) the output to create a new file (uidpid.txt) containing usernames and user IDs.

$ cut -d: -f 1,3 /etc/passwd > uidpid.txt
$cat uidpid.txt
root:0
daemon:1
bin:2
sys:3
sync:4
games:5
man:6
lp:7
mail:8
news:9
uucp:10
proxy:13
www-data:33
backup:34
list:38
irc:39
gnats:41
libuuid:100
syslog:101
messagebus:102
avahi-autoipd:103
usbmux:104
dnsmasq:105
whoopsie:106
kernoops:107
rtkit:108
speech-dispatcher:109
lightdm:110
avahi:111
colord:112
pulse:113
hplip:114
saned:115
abc:1000
nobody:65534

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.