The cut command is a built-in command-line utility which extracts a given number of characters or columns from a file. For cutting a certain number of columns it is important to specify the delimiter. A delimiter specifies how the columns are separated in a text file. Basically, the cut command can cut parts of the line by byte position, field, and character.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Linux System queries.
In this context, we shall look into how to use the cut command in Linux.
Simply put, cut command syntax is given below:
$ cut OPTION… [FILE]...
Its [options] is outlined below:
Here, we are going to look into different ways of using cut command in Linux.
1. -b (byte)
To cut out specific bytes. The range of bytes can be specified with "-". Tabs and backspaces are also considered 1 byte:
$ cut -b [file]
For example, in hello.txt file contains the text "Hello World!". Now we will cut out specific bytes:
$ cut -b 1 hello.txt
Here, the first character will be "H".
Now, when you run the below command, the output will be the 1st, 6th, and 7th characters respectively. The 6th character is an empty character:
$ cut -b 1,6,7 hello.txt
To List with ranges, run the command:
$ cut -b 1-3,7-9 hello.txt
It also selects bytes from beginning up to the end of the line through a special form:
$ cut -b 1- hello.txt
Here, the output from the 1st byte to the last byte will be displayed.
In the below command, the output from the 1st byte to the 4th byte:
$ cut -b -4 hello.txt
2. -f (field)
To extract specific fields. When no delimiter is specified, the default delimiter will be TAB:
$ cut -f [FILE]
For example, here we have field.txt file:
123456 linux mac google
345678 unix ubuntu chrome
Now we will extract the 1st and the 2nd field:
$ cut -f 1,2 field.txt
123456 linux
345678 unix
3. -c (character)
To cut by character. This can be a list separated by “.”, “,”, “;”; “–”. Tabs and backspaces are also considered a character.
$ cut -c [FILE]
For example, here we have character.txt file:
1.3,4,a.r;g;b
Now we will extract the 2nd, 4th, 6th character:
$ cut -c 2,4,6 character.txt
.,,
4. -d (delimiter)
To cut out specific delimiters. This option is often used with -f (field).
For example, here we have dlmt.txt file separated by ":"
abc:456:mac:linux
xyz:ubuntu:win:unix
Now we will extract from the 1st to the 3rd field:
$ cut -d ":" -f 1-3 dlmt.txt
The output will be:
abc:456:mac
xyz:ubuntu:win
5. –complement
To print all fields except those that are not selected with -f option.
For example, here we have field.txt file:
123456 linux mac google
345678 unix ubuntu chrome
Now we will extract all fields except the 1st and 4th fields:
$ cut --complement -f 1,4 field.txt
The output will be:
linux mac
unix ubuntu
6. –output-delimiter
To change the output delimiter. –output-delimiter=”delimiter”.
Now we will extract all fields except the 1st and 4th field but the output is separated by “?” :
& cut –complement -f 1,4 field.txt –output-delimiter=”?”
The Output will be:
linux?mac
unix?ubuntu
This article covers the procedures in which to use the cut command in Linux system. In fact, the cut command in UNIX is a command line utility for cutting sections from each line of files and writing the result to standard output. It can be used to cut parts of a line by byte position, character and delimiter. It can also be used to cut data from file formats like CSV.