BASH

Linux cut Examples

In this post, we feature comprehensive Linux cut Examples. Cut is a Unix command line utility which is used to extract sections from each line of input, usually from a file. It is currently part of the GNU coreutils package and the BSD Base System. It first appeared in AT&T System III UNIX in 1982. Extraction of line segments can typically be done by bytes (-b), characters (-c), or fields (-f) separated by a delimiter (-d — the tab character by default).

A range must be provided in each case which consists of one of N, N-M, N- (N to the end of the line), or -M (beginning of the line to M), where N and M are counted from 1 (there is no zeroth value). Basically the cut command slices a line and extracts the text.

In this article, we will start with a few basic examples and then explore all the advanced options that ‘cut’ provides. First consider the below text file as an example:

>cat example.txt
I want to learn linux, learn linux, learn linux.
Linux is the best
Linux forever

1. Printing characters by position

The cut command can be used to print characters in a line by specifying the position of the characters. To print the characters in a line, use the -c option in cut command. The below cut command prints the fourth character in each line of the file. You can also print more than one character at a time by specifying the character positions in a comma separated list.

>cut -c4 example.txt

2. Printing characters by range

You can print a range of characters in a line by specifying the start and end position of the characters. The below cut command prints the characters from fourth position to the seventh position in each line:

>cut -c4-7 example.txt

3. Printing fields using ‘comma’ delimiter

You can use the cut command just as awk command to extract the fields in a file using a delimiter. The -d option in cut command can be used to specify the delimiter and -f option is used to specify the field position.
The below cut command uses comma as a delimiter and prints the second and third field in each line:

>cut -d, -f2,3 example.txt

The below image illustrates the execution of the first 3 cut commands:

Linux cut Examples: Cut Examples 1-3
Cut Examples 1-3

4. Printing fields using space delimiter

The below cut command prints the second field in each line by treating the space as delimiter. You can also print more than one fields by specifying the position of the fields in a comma delimited list.

>cut -d' ' -f2 example.txt

5. Displaying range of fields

You can print a range of fields by specifying the start and end position. The below cut command prints the first, second and third fields:

>cut -d' ' -f1-3 example.txt

6. Displaying first field from file

The /etc/passwd is a delimited file and the delimiter is a colon (:). The cut command to display the first field in /etc/passwd file is the following:

>cut -d':' -f1 /etc/passwd

The below image illustrates the execution of the previous 3 cut commands:

Linux cut Examples: Cut Examples 4-6
Cut Examples 4-6

7. Displaying fields from 1st to nth

As we already shown in 5th example, a range of fields can be printed by specifying the start and end position. There is also a possibility to omit the start position and declare only the last. The below cut command prints all fields from first to 3rd using space as a delimiter:

>cut -d' ' -f-3 example.txt

8. Displaying fields from nth to last

You can also print all fields by omitting the last position and declaring only the start. The below cut command prints from second field until the last one using space as delimiter:

>cut -d' ' -f2- example.txt

9. Ignoring lines that do not contain delimiter

The cut command can be also be used to print only the lines that do not contain a certain delimiter by using the -s option. The below cut command prints only the lines which contain the comma delimiter and fields from 2nd to last:

>cut -s -d' ' -f2- example.txt

The below image illustrates the execution of the previous 3 cut commands:

Linux cut Examples: Cut Examples 7-9
Cut Examples 7-9

10. Inverting field selection

The –complement option can be used with the cut command in order to print the inverted fields declared with the -f option. The below cut command prints all except for the 2nd field from each line using space as a delimiter:

>cut --complement -d' ' -f2 example.txt

11. Specifying delimiter to be used in output

The previous cut commands which were used to print fields using a delimiter can be modified to have a different delimiter as output, for example use space as delimiter to find fields but print the fields using comma as a delimiter. The below cut command uses space as a delimiter and prints the second and third fields from each line but the output contains comma separated fields:

>cut --output-delimiter=, -d' ' -f2,3 example.txt

12. Printing bytes

Similarly as printing characters using the cut command, you can also print bytes using the -b option. The below cut command prints from first to fourth byte from each line:

>cut -b1-4 example.txt

The below image illustrates the execution of the last 3 cut commands:

Linux cut Examples: Cut Examples 10-12
Cut Examples 10-12

man cut

We tried to present some basic examples of using the cut command. Of course you may always use the man command to find the full functionality of the linux cut command:

>man cut

Nassos Hasiotis

Nassos has graduated from Computer Engineering and Informatics Department in the University of Patras. He also holds a Master degree in Communication and Network Systems from University of Athens. He has a 10-year work experience as a Java and C++ developer in the Telecommunication and IT industry participating in various projects. He currently works as a senior software developer in the IT sector where he is mainly involved with projects for the EU requiring extensive java skills.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Larry David Wannabe
Larry David Wannabe
8 years ago

These examples are nice, but I don’t care for the format of showing screenshots after 3 commands. It’s just a little distracting. Nice tutorial, though.

Back to top button