Cheat Sheet: Cutting Text with cut

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

 $ cut -d: -f1 passwd.bak 
   root
   daemon
 $ head -1 /etc/passwd | cut -d: -f1 
   root

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

 $ cut -d: -f1,2,7 passwd.bak 
  root❌/bin/bash
 $ cut -d  -f3 /etc/motd 
  Ubuntu

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- 
  ping google.com
 $ cut -d  -f-4 /etc/motd 
  Welcome to Ubuntu 12.04
 $ cut -d: -f5-8 passwd 
  root:/root:/bin/bash

Output with a different delimiter (make a CSV)

Convert : to ,
 $ cut --output-delimiter=, -d: -f1- passwd 
  root,x,0,0,root,/root,/bin/bash
Convert space to ,
 $ ps -elf | cut --output-delimiter=, -d -f1-
 F,S,UID,,,,,,,,PID,,PPID,,C,PRI,,NI,ADDR,SZ,WCHAN,,STIME,TTY,,,,,,,,,,TIME,CMD
 4,S,root,,,,,,,,,1,,,,,0,,0,,80,,,0,-,,6083,poll_s,19:07,?,,,,,,,,00:00:12,/sbin/init

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.
 root❌0:0:root:/root:/bin/bash
 daemon❌1:1:daemon:/usr/sbin:/bin/sh
 bin❌2:2:bin:/bin:/bin/sh
 sys❌3:3:sys:/dev:/bin/sh
 sync❌4:65534:sync:/bin:/bin/sync

 $ cut -s -d: -f1-4 tmpfile
 root❌0:0
 daemon❌1:1
 bin❌2:2
 sys❌3:3
 sync❌4:65534

Bytes

 $ cut -b1-45 /etc/motd
 Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-
 $ cut --complement -b10-45 /etc/motd
 Welcome t29-generic x86_64)

Characters

 $ cut -c1-25 /etc/motd
 Welcome to Ubuntu 12.04 L
 $ cut --complement -c10-25 /etc/motd
 Welcome tTS (GNU/Linux 3.2.0-29-generic x86_64)
Avatar
Benjamin Cane
Principal Engineer, Vice President

Benjamin Cane is Principal Engineer at American Express. He has more than 16 years of experience with roles in both systems and software engineering. He leverages both his systems and software skills to build end-to-end platforms. Platforms, purpose built for performance and resiliency. Benjamin is also the author of Red Hat Enterprise Linux - Troubleshooting Guide (2015, Packt Publishing), and he has published many popular articles on topics such as Linux, Docker, Python, Go and Performance Tuning. Thoughts and Opinions expressed in my articles are my own.

Next
Previous

Related