Linux Text Cutting

LinuxLinuxBeginner
Practice Now

Introduction

Welcome to the Linux Text Cutting Lab, set within the enchanted halls of Hogwarts School of Witchcraft and Wizardry. In this lab, you're tasked with assisting a brilliant but forgetful Potions Master, who has stored countless potion recipes in a vast text file. However, the recipes are mixed with notes and other information, causing difficulty in finding the potions' ingredients. The goal of this scenario is to extract only the necessary details using the cut command – a powerful text-processing tool in Linux – so the Potions Master can easily identify the list of ingredients for each potion.

This will not only help the Potions Master in his magical concoctions but will also provide you with the ability to manipulate text files efficiently in a Linux environment.

Are you ready to cast some text-manipulating spells?


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/BasicFileOperationsGroup -.-> linux/cut("`Text Cutting`") subgraph Lab Skills linux/cut -.-> lab-271259{{"`Linux Text Cutting`"}} end

Understanding the cut Command

In this step, we'll explore the basic usage of the cut command to extract text from files or streams. You'll practice by retrieving specific fields from a potion ingredient file, separated by a delimiter.

Create a file named potions.txt in the ~/project directory with different potion recipes as rows and ingredients separated by colons:

echo "Polyjuice Potion:Mandrake root:Slytherin's secret:Knotgrass:Leech juice:Powdered Bicorn horn:Lacewing flies:Boomslang skin" > ~/project/potions.txt
echo "Wolfsbane Potion:Moonseed plant:Lavender:Mistletoe berry:Silverweed roots:Spirit of hellebore:Wolfsbane" >> ~/project/potions.txt

To extract the first ingredient from each recipe, use the cut command like this:

cut -d':' -f2 ~/project/potions.txt

In this example, -d':' specifies the colon as the delimiter and -f2 selects the second field, which corresponds to the first ingredient in our list.

Here is what you should see as the output:

Mandrake root
Moonseed plant

Cutting by Character Position

After mastering the basics of cut by delimiter, let's now focus on extracting text by character position. Our Potions Master needs the initials of the ingredients for labelling.

You'll cut the first character from each ingredient in the potion recipes. Here is how to do it:

Create a new file named ingredient_initials.txt in the ~/project directory and extract the initials using a loop with cut:

while IFS= read -r line; do
  echo "$line" | cut -d':' -f2- | tr ':' '\n' | cut -c1 | tr '\n' ' '
  echo
done < ~/project/potions.txt > ~/project/ingredient_initials.txt

This script changes the delimiter to a newline character using tr, cat -c1 cuts the first character of each line, and then translates the newline back to space for a single line of initials per potion.

View the result using cat:

cat ~/project/ingredient_initials.txt

You should see something like this:

M S K L P B L B
M L M S S H W

Summary

In this lab, you've learned how to use the cut command to extract and manipulate text data in a Linux environment. Focusing on the tasks at hand made it easier to unlock the practical uses of the cut command, such as slicing by delimiter and character position. You can now apply these skills to a range of text processing scenarios. Your success in this lab has greatly impressed the Potions Master and he has awarded you the title of Honorary Assistant Brewer - a testament to your new Linux expertise!

Other Linux Tutorials you may like