Linux cut command with practical examples

The Linux cut command, extracts the sections of text from the input lines of files.

The general syntax

cut [OPTION] [FILE]

The cut command offers special characters like - and , to extract the range and selection of data from input line or stream of lines read from a file.

The cut command has some most useful and quick options to extract data from the input lines..

First such option is -b, --bytes=LIST from the Specified LIST

Using the -b option we shall be able to extract the specific byte characters.

$ echo -e "Exploring the practical use of cut command"

For getting the specific byte index from input stream list:

echo -e "Exploring the practical use of cut command"  | cut -b 3
p

Extracting the range of bytes from input stream list:

[vamshi@linuxcent ~]$ echo -e "Exploring the practical use of cut command"  | cut -b 3-6
plor

Extracting the bytes from starting till the given index range

[vamshi@linuxcent ~]$ echo -e "Exploring the practical use of cut command"  | cut -b -6
Explor

Extracting the bytes from given index range till the end of the input line

[vamshi@linuxcent ~]$ echo -e "Exploring the practical use of cut command"  | cut -b 3-
ploring the practical use of cut command

Combining the , and – operations and extracting the bytes from the input lines:

[vamshi@node02 Linux-blog]$ echo -e "Exploring the practical use of cut command"  | cut -b 1,2-6,9
Explorg

First such option is -c, --characters=LIST from the Specified LIST

Extracting the specific character from the input string:

[vamshi@node02 Linux-blog]$ echo -e "Exploring the practical use of cut command"  | cut -c 11
t

Extracting the range of bytes from input stream list:

[vamshi@linuxcent ~]$ echo -e "Exploring the practical use of cut command"  | cut -c 11-24
the practical

Getting the range of the characters from given index till the end of the input line using -

[vamshi@node02 Linux-blog]$ echo -e "Exploring the practical use of cut command"  | cut -c 11-

the practical use of cut command

Using - to get the characters from starting till the given index range

[vamshi@node02 Linux-blog]$ echo -e "Exploring the practical use of cut command"  | cut -c -9

Exploring

We have a Text file called README with the following content.

The following are Most popular Linux server Distributions and their curent versions:

Distribution Versions
----------- --------
Redhat : 8.0
Ubuntu : 18.04
Centos : 8.0
Debian : 10.1
SUSE : 15.1

Extracting the fields from the given data file using -f, separated by the tabs which is a default delimited.

Lets try and extract the field from README datafile.

$ cat README | cut -f1
[vamshi@linuxcent ~]$ cut -f1 README
The following are Most popular Linux server Distributions and their current versions:

Distribution
-----------
Redhat
Ubuntu
Centos
Debian
SUSE

The options and ranges can also be mentioned in the field option to extract relevant data.
Extracting the fields indexed by numbers from given file separated by tabs

$ cat README | cut -f1,2,3
$ cut -f1,2,3 README

Extracting the first field through 3rd field from the given file separated by tabs

$ cat README | cut -f1-3 
$ cut -f1-3 README

A lot of combinations can be used to extract the ranges as shows below:

$ cat README | cut -f 3- 

We will also like to shed some light on the --complement option, As the option name hints, it will print only the inverse of the operation, It can be used in conjunction with -b , -c and -f respectively:

For example

[vamshi@linuxcent Linux-blog]$ echo -e "Exploring the practical use of cut command" | cut -c1 --complement
xploring the practical use of cut command

The first character E is excluded and then remaining characters are printed because of –complement option being used in conjunction.

Delimiter Option -d, --bytes=DELIM from the Specified LIST
Unlike the field extractor option which only set to default single tab space as Delimiter, The Delimiter option provides the flexibility to setup a custom Delimiter to any character. Widely used for setting a Delimiter other than a Tab.

Lets take an example of the line following input line:

dockerroot:x:996:992:Docker User:/var/lib/docker:/sbin/nologin

The delimit option -dcan be set to :  and have the combination of fields and characters/bytes to highly customize and refine the extract text.

[vamshi@linuxcent Linux-blog]$ cat /etc/passwd | grep docker | cut -d":" -f1
dockerroot
[vamshi@linuxcent Linux-blog]$ cat /etc/passwd | grep docker | cut -d"/" -f1-5
dockerroot:x:996:992:Docker User:/var/lib/docker:/sbin

The same logic of obtaining and extracting the ranges and specific field extractions is applied commonly in cut command.

A very special mentioning of the operator -s or --only-delimited, Which prints only the lines modified by the cut command and delimited by the delimit operator.

[vamshi@linuxcent Linux-blog]$ cat README | cut -d":" -f1 -s
The following are Most popular Linux server Distributions and their curent versions
Redhat 
Ubuntu 
Centos 
Debian 
SUSE

The option --output-delimiter="STRING" takes a string as its input and then substitutes it with the actual delimiter:

Lets look at the below practical example:

[vamshi@node02 Linux-blog]$ cat /etc/passwd | grep docker | cut -d":" -f1- --output-delimiter=' '
dockerroot x 996 992 Docker User /var/lib/docker /sbin/nologin

Format the Output to print newline using cut command:

[vamshi@linuxcent  ~]$ cat /etc/passwd | grep docker | cut -d":" -f1- --output-delimiter=$'\n'
dockerroot
x
996
992
Docker User
/var/lib/docker
/sbin/nologin

The output-delimiter formatting the output by replacing the delimiter with a newline character using $'\n' thus printing the output separated by the newline

How use cut in Linux?

It can be used to cut parts of a line by byte position, character and field. Basically the cut command slices a line and extracts the text. It is necessary to specify option with command otherwise it gives error. If more than one file name is provided then data from each file is not precedes by its file name.

What does the cut command do?

The cut command is a command-line utility for cutting sections from each line of a file. It writes the result to the standard output.

How do I cut a word in Linux?

CUT
Most important Options:

  1. -b, –bytes=LIST # select only these bytes.
  2. -c, –characters=LIST # select only these characters.
  3. -d, –delimiter=DELIM # use DELIM instead of TAB for field delimiter.

What is cut in bash?

The cut command is used to extract the specific portion of text in a file. Many options can be added to the command to exclude unwanted items. It is mandatory to specify an option in the command otherwise it shows an error. In this article, we will throw light on each option of the cut command.

How do you cut on the keyboard?

Keyboard shortcuts

  1. Copy: Ctrl+C.
  2. Cut: Ctrl+X.
  3. Paste: Ctrl+V.

How do you trim in Unix?

Example-2: Trim string data using `sed` command

Use sed ‘s/^ *//g’, to remove the leading white spaces. There is another way to remove whitespaces using `sed` command. The following commands removed the spaces from the variable, $Var by using `sed` command and [[:space:]].

What does cut do Unix?

In computing, cut is a command line utility on Unix and Unix-like operating systems which is used to extract sections from each line of input — usually from a file.

How do you cut a column in Unix?

The Linux cut command allows you to cut data by character, by field, or by column. if used correctly along with sed, find, or grep in UNIX, the cut can do lots of reporting stuff. For example, you can extract columns from a comma-separated file or a pipe or colon-delimited file using cut command.

How do I trim a word in bash?

${var// /} removes all space characters. There’s no way to trim just leading and trailing whitespace with only this construct. Bash “patterns” are regular expressions, only with a different syntax than “POSIX (extended) regular expressions”.

How do I cut a word in bash?

In Linux try Ctrl+k to delete from where the cursor is to the end of the word. There are few other shortcuts listed below(working in Linux): Ctrl+e -> Takes cursor at the end of the word.

Leave a Comment