DEV Community

Cover image for 11.String Replacement
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

11.String Replacement

Lab Information

Within the Stratos DC, the backup server holds template XML files crucial for the Nautilus application. Before utilization, these files require valid data insertion. As part of routine maintenance, system admins at xFusionCorp Industries employ string and file manipulation commands.

Your task is to substitute all occurrences of the string Text with Submarine within the XML file located at /root/nautilus.xml on the backup server.

Lab Solutions

🧭 Part 1: Lab Step-by-Step Guidelines

πŸ”Ή Step 1: Log in to Jump Host

ssh thor@jump_host.stratos.xfusioncorp.com

Password:

mjolnir123

πŸ”Ή Step 2: SSH into Backup Server

ssh clint@stbkp01.stratos.xfusioncorp.com

Password:

H@wk3y3

πŸ”Ή Step 3: Switch to root

sudo -i
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Step 4: Replace all occurrences of Text with Submarine

sed -i 's/Text/Submarine/g' /root/nautilus.xml
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Step 5: Verify replacement

grep Text /root/nautilus.xml
Enter fullscreen mode Exit fullscreen mode

This should return no output.

Then confirm:

grep Submarine /root/nautilus.xml

You should see occurrences of Submarine.

βœ… Final Checklist

βœ” Executed on Backup Server only
βœ” Modified file: /root/nautilus.xml
βœ” All Text replaced with Submarine
βœ” No remaining Text strings
βœ” Verified successfully


🧠 Part 2: Simple Step-by-Step Explanation (Beginner Friendly)

πŸ”Ή What is the task?

Inside this file:

/root/nautilus.xml

There are multiple instances of the word:

Text

We must replace every occurrence with:

Submarine

πŸ”Ή Why use sed?

sed is a stream editor used for:

Searching

Replacing

Editing files directly from the command line

πŸ”Ή Breaking down the command
sed -i 's/Text/Submarine/g' file
Part Meaning
-i Edit file in place
s Substitute
Text Word to find
Submarine Replacement word
g Replace all occurrences in each line

πŸ”Ή Why use g?

Without g, only the first occurrence in each line would be replaced.

With g, every occurrence is replaced.

πŸ” Real-World Context

This simulates:

Updating template files

Injecting correct configuration values

Automating repetitive file edits

String replacement using sed is a common DevOps and sysadmin task.


Resources & Next Steps
πŸ“¦ Full Code Repository: KodeKloud Learning Labs
πŸ“– More Deep Dives: Whispering Cloud Insights - Read other technical articles
πŸ’¬ Join Discussion: DEV Community - Share your thoughts and questions
πŸ’Ό Let's Connect: LinkedIn - I'd love to connect with you

Credits
β€’ All labs are from: KodeKloud
β€’ I sincerely appreciate your provision of these valuable resources.

Top comments (0)