10 Cut Command Linux Examples: Mastering Text Extraction

Photo of author

By rasho

Introduction

Linux offers a wide range of useful text processing tools, and one of the most powerful among them is the ‘cut’ command. This tool allows for precise extraction and manipulation of text based on specific criteria. In this article, we will explore 10 practical examples of using the ‘cut’ command in Linux to successfully extract desired information and process text files.
[ads]

Extracting specific columns from a CSV file

When working with CSV files, the ‘cut’ command is invaluable. For example, if you want to extract only the first and third columns from a file, use the following command:

cut -d',' -f1,3 file.csv

Extracting a specific number of characters from a line of text

If you want to extract a specific number of characters from a line of text, use the ‘-c’ option. For example, extracting the first 10 characters from a file:

cut -c1-10 file.txt

Extracting fields from a log file

The role of the ‘cut’ command also extends to extracting fields from log files. For instance, extracting only the IP addresses from Apache access logs:

cut -d' ' -f1 access.log

Splitting text based on a delimiter

Sometimes, you may want to split a line of text into parts based on a specific delimiter. The ‘cut’ command can easily accomplish that. For example, splitting text based on spaces:

cut -d' ' -f1,2 file.txt

Extracting fields from the /etc/passwd file

If you need to extract specific fields from the /etc/passwd file, the ‘cut’ command is the right tool for the job. For example, extracting usernames and shells:

cut -d':' -f1,7 /etc/passwd

Extracting text between specific characters

The ‘cut’ command also allows for extracting text between specific characters. For example, extracting text between square brackets:

cut -d'[' -f2 file.txt | cut -d']' -f1

Extracting the last field from each line of text

If you want to extract only the last field from each line of text, the ‘cut’ command can achieve that. For example:

cut -d' ' -f4 file.txt

Extracting fields from fixed-width text

When working with fixed-width text files, the ‘cut’ command provides a powerful mechanism for extracting fields. For example, extracting the first 10-character field:

cut -c1-10 file.txt

[ads]

Extracting fields with a specified character offset

If you want to skip a certain number of characters before extracting fields, the ‘cut’ command offers the ‘-b’ option. For example, extracting fields after skipping the first 5 characters:

cut -b6- file.txt

Extracting unique lines from a file

If you only want to extract unique lines from a file, the ‘cut’ command can help in conjunction with ‘sort’ and ‘uniq’ tools. For example:

cut -d' ' -f1 file.txt | sort | uniq

Conclusion

The ‘cut’ command is a powerful tool for text extraction and processing in Linux. Through these 10 examples, you have gained fundamental knowledge of its usage and discovered how to manipulate data, extract specific parts of text, and customize output according to your needs. We hope these examples will be valuable in your everyday Linux system administration tasks.

Leave a Comment