Table of Contents


The basics of cut

echo “This is a line of text” | cut -c 1-10 cut -c 1-10 cut -c 11- 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 ' '
$ echo "abc    def" | cut -f 2 -d ' '

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


Copyright © 2020-2021 Derek Taylor (DistroTube)

This page is licensed under a Creative Commons Attribution-NoDerivatives 4.0 International License (CC-BY-ND 4.0).

The source code for can be found on GitLab. User-submitted contributions to the site are welcome, as long as the contributor agrees to license their submission with the CC-BY-ND 4.0 license.

Author: dt

Created: 2022-02-20 Sun 10:16