Linux cut Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux cut command to extract specific columns or fields from text files or command outputs. The lab covers the basic syntax and options of the cut command, as well as practical examples of extracting data from files and using the command with pipes. By the end of the lab, you will be able to efficiently manipulate and process text data using this powerful command-line tool.

The lab consists of three main steps: understanding the cut command syntax and options, extracting specific columns from a text file, and using cut with pipes to process data. These steps provide a comprehensive introduction to the cut command and its various use cases, equipping you with the knowledge to effectively work with text data in a Linux environment.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicFileOperationsGroup -.-> linux/cut("`Text Cutting`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/TextProcessingGroup -.-> linux/awk("`Text Processing`") subgraph Lab Skills linux/cat -.-> lab-422626{{"`Linux cut Command with Practical Examples`"}} linux/cut -.-> lab-422626{{"`Linux cut Command with Practical Examples`"}} linux/echo -.-> lab-422626{{"`Linux cut Command with Practical Examples`"}} linux/awk -.-> lab-422626{{"`Linux cut Command with Practical Examples`"}} end

Understand the cut Command Syntax and Options

In this step, you will learn about the syntax and options of the cut command in Linux. The cut command is a powerful tool for extracting specific columns or fields from a text file or the output of a command.

To understand the basic syntax of the cut command, let's start with a simple example:

cut -d ' ' -f 2,4 file.txt

In this command:

  • cut is the command name
  • -d ' ' specifies the delimiter (in this case, a space) to use when splitting the input
  • -f 2,4 tells cut to extract the 2nd and 4th fields (columns) from each line

Example output:

field2 field4
another_field2 another_field4

The cut command also supports several other options:

  • -c: Extract characters instead of fields
  • -b: Extract bytes instead of fields
  • --complement: Select the complement of the set of bytes, characters or fields
  • -s: Only output lines with delimiter characters

Let's try another example using the -c option to extract characters:

cut -c 1-5,10-15 file.txt

Example output:

field
another_field

This command extracts the characters from positions 1 to 5, and 10 to 15 from each line in the file.

Extract Specific Columns from a Text File

In this step, you will learn how to use the cut command to extract specific columns from a text file.

Let's start by creating a sample data file:

echo "Name,Age,City" > data.txt
echo "John,25,New York" >> data.txt
echo "Jane,30,London" >> data.txt
echo "Bob,35,Paris" >> data.txt

Now, let's extract the name and city columns from the file:

cut -d ',' -f 1,3 data.txt

Example output:

Name,City
John,New York
Jane,London
Bob,Paris

In this command:

  • -d ',' specifies the comma as the delimiter to split the input
  • -f 1,3 tells cut to extract the 1st and 3rd fields (columns)

You can also use the --output-delimiter option to change the delimiter in the output:

cut -d ',' -f 1,3 --output-delimiter=' - ' data.txt

Example output:

Name - City
John - New York
Jane - London
Bob - Paris

Now, let's try extracting a range of columns:

cut -d ',' -f 2-3 data.txt

Example output:

Age,City
25,New York
30,London
35,Paris

This command extracts the 2nd and 3rd columns (Age and City) from the data.

Use cut Command with Pipe to Process Data

In this step, you will learn how to use the cut command in combination with pipes to process data from various sources.

Let's start by creating a sample data file:

echo "Name,Age,City" > data.txt
echo "John,25,New York" >> data.txt
echo "Jane,30,London" >> data.txt
echo "Bob,35,Paris" >> data.txt

Now, let's use the cut command with a pipe to extract the name and city columns from the data:

cat data.txt | cut -d ',' -f 1,3

Example output:

Name,City
John,New York
Jane,London
Bob,Paris

In this example, we use the cat command to display the contents of the data.txt file, and then pipe the output to the cut command to extract the desired columns.

You can also use the cut command with other commands, such as grep, to filter the data:

cat data.txt | grep "New York" | cut -d ',' -f 1,3

Example output:

Name,City
John,New York

This command first uses grep to filter the data for lines containing "New York", and then uses cut to extract the name and city columns from the filtered output.

Another example is using cut with awk to perform more complex data processing:

cat data.txt | awk -F ',' '{print $1, "is", $2, "years old and lives in", $3}' | cut -d ' ' -f 1,3,5,7

Example output:

John is 25 years old and lives in New York
Jane is 30 years old and lives in London
Bob is 35 years old and lives in Paris

In this example, we use awk to split the input by the comma delimiter and construct a new output string, which we then pass through cut to extract the desired fields.

Summary

In this lab, you learned about the syntax and options of the cut command in Linux, which is a powerful tool for extracting specific columns or fields from a text file or the output of a command. You also learned how to use the cut command to extract specific columns from a text file, including the ability to change the delimiter in the output.

The lab covered the following key points:

  1. Understanding the basic syntax of the cut command, including the use of the -d option to specify the delimiter and the -f option to select the fields to extract.
  2. Exploring additional options such as -c to extract characters instead of fields, and --complement to select the complement of the set of bytes, characters, or fields.
  3. Demonstrating how to extract specific columns from a text file using the cut command, and how to customize the output delimiter using the --output-delimiter option.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like