Cut

Table of Contents

Home DTOS Knowledge Base Community Contribute Support DT’s Work

cut

The basics of cut

echo “This is a line of text” | cut -c 1-10 cut -c 1-10 test.sh cut -c 11- test.sh echo “This is a line of text” | cut -d ’ ’ -f5 echo “This:is:a:line:of:text” | cut -d ’:’ -f5 echo “This is a line of text” | cut -d ’ ’ -f5-

Compared to awk

cut takes a single character in -d as the field delimiter (the default being TAB), and every single occurrence of that character starts a new field. awk, however, is more flexible. The separator is in the FS variable and can be an empty string (every input character makes a separate field), a single character, or a regular expression. The special case of a single space character (the default) means to split on any sequence of whitespace. Also, awk suppresses leading whitespace by default.

$ echo "abc def" | cut -f 2 -d ' '
def
$ echo "abc    def" | cut -f 2 -d ' '

$ echo " abc def" | cut -f 2 -d ' '
abc
$ echo "abc def" | awk '{ print $2 }'
def
$ echo "abc    def" | awk '{ print $2 }'
def
$ echo " abc def" | awk '{ print $2 }'
def

Author: Derek Taylor

Created: 2021-08-27 Fri 15:34