0
0
Linux CLIscripting~15 mins

sed substitution in Linux CLI - Mini Project: Build & Apply

Choose your learning style9 modes available
Basic Text Replacement Using sed Substitution
📖 Scenario: You have a text file that contains a list of fruits. You want to replace one fruit name with another using a simple command.
🎯 Goal: Learn how to use the sed command to substitute text in a file and display the changed content.
📋 What You'll Learn
Create a text file named fruits.txt with specific fruit names.
Define the fruit name to replace using a variable.
Use sed substitution to replace the fruit name in the file content.
Display the updated content on the screen.
💡 Why This Matters
🌍 Real World
Text substitution is common when updating configuration files, logs, or data files automatically without opening them manually.
💼 Career
Knowing how to use sed substitution helps in system administration, automation scripts, and data processing tasks.
Progress0 / 4 steps
1
Create the text file with fruit names
Create a file named fruits.txt with these exact lines:
apple
banana
cherry
Linux CLI
Need a hint?

Use echo with -e and redirect output to fruits.txt.

2
Set the fruit name to replace
Create a variable called old_fruit and set it to banana.
Linux CLI
Need a hint?

Use old_fruit=banana to assign the variable.

3
Use sed to substitute the fruit name
Use sed with substitution to replace $old_fruit with orange in fruits.txt and save the result to a variable called new_content.
Linux CLI
Need a hint?

Use sed "s/$old_fruit/orange/g" fruits.txt and command substitution $( ).

4
Display the updated content
Print the variable new_content to show the updated fruit list with banana replaced by orange.
Linux CLI
Need a hint?

Use echo "$new_content" to print the updated text.