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 delimiter
Template:
cut -d\<delimiter\> -f\<part-to-extract\>
Example: split the string "foo bar baz"
Extract the first part
$ echo "foo bar baz" | cut -d' ' -f1 # outputs "foo"
Extract the third part
$ echo "foo bar baz" | cut -d' ' -f3 # outputs "bar"
Extract the sixth part (oops)
$ echo "foo bar baz" | cut -d' ' -f6 # outputs nothing because there aren't 6 parts available
Split on one or more spaces
This is useful to parse the output of
ls
, because the output is not evenly spaced
Use tr
first and pipe the result to cut
:
$ echo "foo bar baz" | tr -s ' ' | cut -d' ' -f2
# outputs "bar"
Example: parse the output of ls -lha
:
original command output
$ 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
Extract the month name (6th block, so use
-f6
)$ ls -lha ~/ | grep profile | tr -s ' ' | cut -d' ' -f6 Abr Jul