0
0
Bash Scriptingscripting~30 mins

sed for substitution in scripts in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Using sed for Substitution in Scripts
📖 Scenario: You are working on a script that processes a text file containing product names and prices. You want to update the prices by increasing them by 10% using sed substitution commands.
🎯 Goal: Build a bash script that uses sed to substitute old prices with new prices in a text file.
📋 What You'll Learn
Create a text file variable with product names and prices
Create a variable for the price increase percentage
Use sed substitution to update prices by 10%
Print the updated text to the terminal
💡 Why This Matters
🌍 Real World
Automating price updates in product lists or configuration files without manual editing.
💼 Career
Many automation and scripting jobs require text processing and substitution using tools like sed.
Progress0 / 4 steps
1
Create the initial product list
Create a variable called products that contains these exact lines as a multiline string:
Apple 100
Banana 50
Cherry 75
Bash Scripting
Need a hint?

Use double quotes and \n for new lines inside the string.

2
Set the price increase percentage
Create a variable called increase and set it to 10 to represent a 10% price increase.
Bash Scripting
Need a hint?

Just assign the number 10 to the variable increase.

3
Use sed to increase prices by 10%
Use a sed command to substitute each price in products by increasing it by 10%. Use the echo command to pass products to sed. Store the result in a variable called updated_products. Use this exact sed expression: sed -E 's/([0-9]+)/echo $((\1 * 110 / 100))/ge'
Bash Scripting
Need a hint?

The sed command uses s/([0-9]+)/echo $((\1 * 110 / 100))/ge to replace each number with the increased price.

4
Print the updated product list
Print the variable updated_products to display the updated product names and prices.
Bash Scripting
Need a hint?

Use echo "$updated_products" to print the updated list.