0
0
Linux CLIscripting~20 mins

sed (stream editor) basics in Linux CLI - Mini Project: Build & Apply

Choose your learning style9 modes available
sed (stream editor) basics
📖 Scenario: You have a text file with a list of fruits and their quantities. You want to practice using sed to edit this file's content without opening a text editor.
🎯 Goal: Learn how to use sed commands to replace text, delete lines, and print specific lines from a file.
📋 What You'll Learn
Use sed commands to modify text
Replace specific words in the file
Delete a line containing a certain word
Print only a specific line from the file
💡 Why This Matters
🌍 Real World
Editing configuration files or logs quickly without opening an editor is common in system administration and automation.
💼 Career
Knowing sed helps automate text processing tasks, saving time and reducing errors in many IT and development roles.
Progress0 / 4 steps
1
Create the initial text file
Create a file named fruits.txt with these exact lines:
apple 10
banana 5
cherry 20
date 7
elderberry 3
Linux CLI
Need a hint?

Use echo -e with newline characters \n and redirect output to fruits.txt.

2
Set the word to replace
Create a variable called old_word and set it to banana.
Create another variable called new_word and set it to blueberry.
Linux CLI
Need a hint?

Use simple variable assignment like old_word=banana.

3
Replace the word using sed
Use sed with the s command to replace all occurrences of $old_word with $new_word in fruits.txt and save the output to a new file called fruits_updated.txt.
Linux CLI
Need a hint?

Use sed "s/$old_word/$new_word/g" fruits.txt > fruits_updated.txt to replace all occurrences.

4
Print the updated file content
Use cat to display the contents of fruits_updated.txt.
Linux CLI
Need a hint?

Use cat fruits_updated.txt to see the updated file.