Unix cut command: Reference and Examples
Last updated:Table of Contents
WIP Alert This is a work in progress. Current information is correct but more content may be added in the future.
Split by space
-f
index starts at 1!
To split by one or more spaces, use tr -s ' ' | cut -d' ' -f
to replace multiple spaces with a single one and then use cut:
$ echo "foo bar baz" | tr -s ' ' | cut -d' ' -f2
# outputs "bar"
Custom delimiter
-f
index starts at 1!
Template cut -d<delimiter> -f<part-to-extract>
Example: split the string "fooXbarXbaz"
and extract parts
Extract the first part
$ echo "fooXbarXbaz" | cut -d'X' -f1 # outputs "foo"
Extract the sixth part (ERROR)
$ echo "fooXbarXbaz" | cut -d'X' -f6 # outputs nothing because there aren't 6 parts available
Split output of ls
Original output of ls -lha
$ ls -lha ~/ | grep profile
-rw-rw-r-- 1 felipe felipe 200 Abr 21 2017 .bash_profile
-rw-r--r-- 1 felipe felipe 1000 Jul 5 05:21 .profile
Example: Extract the month name (6th block, so use -f6
)
$ ls -lha ~/ | grep profile | tr -s ' ' | cut -d' ' -f6
Abr
Jul