cut
"cut out selected portions of each line of a file" - man cut
Examples
keep only the selected characters
cut
is 1-indexed
$ echo {0..9}
0 1 2 3 4 5 6 7 8 9
$ echo {0..9} | cut -c 1
0
$ echo -n {0..9} | cut -c 1-5
0 1 2
$ echo -n {0..9} | cut -c 1,19
09
$ echo -n {a..z} | sed 's/ //g' | cut -c 10-20
jklmnopqrst
keep only the given field index
By default, cut
works with tab delimited fields, which is not really useful. You can specify a different field delimiter with -d
$ echo {a..z} | cut -d ' ' -f 3
c
$ echo {a..z} | cut -d ' ' -f 3-10
c d e f g h i j
$ echo 'a^b^c' | cut -d'^' -f 3
c
It's not really intuitive though because every single space separates one field from the next, even if the field is null
$ echo 'a b c d' | cut -d ' ' -f 1-3
a b c
$ echo 'a b c d' | cut -d ' ' -f 1-3
a b
Using -w
allows all consecutive whitespace to be treated as one separator, which is usually the desired behavior, but then the output fields are separated by tabs.
$ echo 'a b c d' | cut -w -f 1-3
a b c
See Also
cut
is often used with[tr](tr.md)
[awk](awk.md)
is what I usually reach for instead ofcut
when working with words.