Cheat Sheet: Cutting Text with cut

Written by Benjamin Cane on 2012-10-22 16:30:01 | 1 min read

The cut command is a Unix/Linux tool used to literally cut text from files and output from other commands. With the cut command a user can take text and output only certain parts of the line.

In my opinion cut is the most under recognized and utilized command in Linux/Unix. This is mostly due to the fact that when most Sysadmins want to cut text from files or standard output many will reach for AWK.

While AWK is a great tool for quick and dirty commands; I tend to reach for cut before AWK. The below cheat sheet should show many ways to use cut with every day tasks.

Common Separated Values

Print the first field out of a CSV file

 $ cut -d: -f1 passwd.bak 

Print the first field out of CSV output from another command

 $ head -1 /etc/passwd | cut -d: -f1 

Print the first, second, and seventh field out of a CSV file

To change the field simply change the numbers after -f separated by a comma.

 $ cut -d: -f1,2,7 passwd.bak 

Print text based on Spaces

 $ cut -d  -f3 /etc/motd 

Print the fourth field and everything after that from a command

This command is pretty handy if you wanted to make a script out of the past few commands you ran.

 $ history | cut -d  -f 4- 

Print up to the 4th field

 $ cut -d  -f-4 /etc/motd 
  Welcome to Ubuntu 12.04

Print all fields between 5 and 8

 $ cut -d: -f5-8 passwd 

Output with a different delimiter (make a CSV)

Convert : to ,
 $ cut --output-delimiter=, -d: -f1- passwd 

Convert space to ,
 $ ps -elf | cut --output-delimiter=, -d -f1-

Output only lines that have a delimiter

This command will only output lines that have a : (in our example), within the file tmpfile there are multiple lines some with : and some without.

 $ head tmpfile
 Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-29-generic x86_64)
 73 packages can be updated.
 10 updates are security updates.

 $ cut -s -d: -f1-4 tmpfile


Print bytes 1 through 45

 $ cut -b1-45 /etc/motd
 Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-

Print everything but bytes 10 through 45

 $ cut --complement -b10-45 /etc/motd
 Welcome t29-generic x86_64)


Print characters 1 through 25

 $ cut -c1-25 /etc/motd
 Welcome to Ubuntu 12.04 L

Print everything but characters 10 through 25

 $ cut --complement -c10-25 /etc/motd
 Welcome tTS (GNU/Linux 3.2.0-29-generic x86_64)

Picture of Benjamin Cane

Benjamin is a Systems Architect working in the financial services industry focused on platforms that require Continuous Availability. He has been working with Linux and Unix for over 10 years now and has recently published his first book; Red Hat Enterprise Linux Troubleshooting Guide.


Identify, capture and resolve common issues faced by Red Hat Enterprise Linux administrators using best practices and advanced troubleshooting techniques

What people are saying:
Excellent, excellent resource for practical guidance on how to troubleshoot a wide variety of problems on Red Hat Linux. I particularly enjoyed how the author made sure to provide solid background and practical examples. I have a lot of experience on Red Hat but still came away with some great practical tools to add to my toolkit. - Amazon Review

Sponsored by