DEV Community

Janak Shrestha
Janak Shrestha

Posted on

String Replacement

At xFusionCorp Industries, the Stratos Datacenter houses a jump host server that stores template XML files essential for the Nautilus application. Prior to their use, these files need to be populated with valid data. As part of regular maintenance, the system administration team utilizes various string and file manipulation commands to prepare these templates.

Your task is to substitute all occurrences of the string Text with Echo-Location within the XML file located at /root/nautilus.xml on the jump host server.


Solution

Step 1: Connect to the Jump Host Server

ssh thor@jump-host
# Password: mjolnir123
Enter fullscreen mode Exit fullscreen mode

Step 2: Switch to root

sudo su -
# Password: mjolnir123
Enter fullscreen mode Exit fullscreen mode

Step 3: Verify the file exists and check its content

# Check if file exists
ls -la /root/nautilus.xml

# View the file content (optional)
cat /root/nautilus.xml
Enter fullscreen mode Exit fullscreen mode

Step 4: Substitute all occurrences of "Text" with "Echo-Location"

Method 1: Using sed (Recommended)

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

Command breakdown:

  • sed: Stream editor for filtering and transforming text
  • -i: Edit files in-place (without backup)
  • s/Text/Echo-Location/g: Substitute all occurrences
    • s: Substitute command
    • Text: Pattern to search for
    • Echo-Location: Replacement string
    • g: Global (replace all occurrences, not just the first)

Method 2: Using sed with backup (Safer)

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

This creates a backup file nautilus.xml.bak before making changes.

Step 5: Verify the changes

# View the modified file
cat /root/nautilus.xml

# Check for any remaining "Text" strings
grep -n "Text" /root/nautilus.xml

# Check for "Echo-Location" strings
grep -n "Echo-Location" /root/nautilus.xml

# Count occurrences replaced
grep -o "Echo-Location" /root/nautilus.xml | wc -l
Enter fullscreen mode Exit fullscreen mode

Complete One-Line Commands

From jump host directly (as root):

sed -i 's/Text/Echo-Location/g' /root/nautilus.xml && echo "✓ Substitution complete" && grep -c "Echo-Location" /root/nautilus.xml
Enter fullscreen mode Exit fullscreen mode

From jump host with sudo:

sudo sed -i 's/Text/Echo-Location/g' /root/nautilus.xml && echo "✓ Substitution complete"
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Interactive Commands

# Connect to jump host
ssh thor@jump-host
# Password: mjolnir123

# Become root
sudo su -
# Password: mjolnir123

# Check the original file
echo "=== Original file content ==="
cat /root/nautilus.xml

# Create a backup (recommended)
cp /root/nautilus.xml /root/nautilus.xml.backup
echo "✓ Backup created at /root/nautilus.xml.backup"

# Perform the substitution
echo "=== Performing substitution ==="
sed -i 's/Text/Echo-Location/g' /root/nautilus.xml
echo "✓ Substitution complete"

# Verify the changes
echo "=== File after substitution ==="
cat /root/nautilus.xml

# Check for any remaining "Text" strings
echo "=== Checking for remaining 'Text' strings ==="
grep -n "Text" /root/nautilus.xml || echo "✓ No 'Text' strings found"

# Count the replacements
echo "=== Count of 'Echo-Location' occurrences ==="
grep -o "Echo-Location" /root/nautilus.xml | wc -l

# Exit
exit
exit
Enter fullscreen mode Exit fullscreen mode

Alternative Commands

1. Using sed with different delimiters (if text contains slashes)

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

2. Using awk

awk '{gsub(/Text/, "Echo-Location"); print}' /root/nautilus.xml > /tmp/nautilus.xml.tmp && mv /tmp/nautilus.xml.tmp /root/nautilus.xml
Enter fullscreen mode Exit fullscreen mode

3. Using perl (if available)

perl -pi -e 's/Text/Echo-Location/g' /root/nautilus.xml
Enter fullscreen mode Exit fullscreen mode

4. Using sed with case-insensitive substitution

sed -i 's/Text/Echo-Location/gi' /root/nautilus.xml
# i = case-insensitive
Enter fullscreen mode Exit fullscreen mode

5. Using sed to replace only whole words (not partial)

sed -i 's/\bText\b/Echo-Location/g' /root/nautilus.xml
# \b matches word boundaries
Enter fullscreen mode Exit fullscreen mode

Verification Commands

Run these to confirm the substitution worked correctly:

# 1. Check if file exists
ls -la /root/nautilus.xml

# 2. View the entire file
cat /root/nautilus.xml

# 3. Check for "Text" occurrences (should be 0)
grep -i "Text" /root/nautilus.xml

# 4. Check for "Echo-Location" occurrences
grep -o "Echo-Location" /root/nautilus.xml | wc -l

# 5. Show lines containing the replacement
grep -n "Echo-Location" /root/nautilus.xml

# 6. Verify with diff (if backup exists)
diff /root/nautilus.xml.backup /root/nautilus.xml

# 7. Check file permissions (ensure unchanged)
ls -la /root/nautilus.xml

# 8. Validate XML syntax (if xmllint is available)
xmllint --noout /root/nautilus.xml && echo "✓ XML is valid"
Enter fullscreen mode Exit fullscreen mode

Example Output

[root@jump-host ~]# cat /root/nautilus.xml
<config>
    <setting name="Text">Value1</setting>
    <setting name="AnotherText">Value2</setting>
    <Text>This is a Text element</Text>
    <description>Replace Text with something else</description>
</config>

[root@jump-host ~]# sed -i 's/Text/Echo-Location/g' /root/nautilus.xml

[root@jump-host ~]# cat /root/nautilus.xml
<config>
    <setting name="Echo-Location">Value1</setting>
    <setting name="AnotherEcho-Location">Value2</setting>
    <Echo-Location>This is a Echo-Location element</Echo-Location>
    <description>Replace Echo-Location with something else</description>
</config>

[root@jump-host ~]# grep -c "Echo-Location" /root/nautilus.xml
4

[root@jump-host ~]# grep -c "Text" /root/nautilus.xml
0
Enter fullscreen mode Exit fullscreen mode

Advanced Options

Create backup and replace with verbose output

sed -i.bak 's/Text/Echo-Location/g' /root/nautilus.xml && \
echo "✓ Substitution complete" && \
echo "Occurrences replaced: $(grep -o 'Echo-Location' /root/nautilus.xml | wc -l)" && \
echo "Backup saved as: /root/nautilus.xml.bak"
Enter fullscreen mode Exit fullscreen mode

Replace only in specific lines (e.g., lines containing "setting")

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

Replace with confirmation (interactive)

sed -i 's/Text/Echo-Location/g' /root/nautilus.xml && \
echo "Changes made. Review the file: cat /root/nautilus.xml"
Enter fullscreen mode Exit fullscreen mode

Replace and show changes (without modifying)

# Preview changes without applying
sed 's/Text/Echo-Location/g' /root/nautilus.xml | diff /root/nautilus.xml -
Enter fullscreen mode Exit fullscreen mode

Troubleshooting

  1. "Permission denied":
   # Use sudo
   sudo sed -i 's/Text/Echo-Location/g' /root/nautilus.xml
   # Or become root
   sudo su -
Enter fullscreen mode Exit fullscreen mode
  1. "No such file or directory":
   # Check if file exists
   ls -la /root/nautilus.xml
   # If not, check other locations
   find / -name "nautilus.xml" 2>/dev/null
Enter fullscreen mode Exit fullscreen mode
  1. "sed: -e expression #1" error:
   # Use different delimiters if the text contains slashes
   sed -i 's|Text|Echo-Location|g' /root/nautilus.xml
Enter fullscreen mode Exit fullscreen mode
  1. Need to preserve XML formatting:
   # Use xmlstarlet for XML-specific operations (if available)
   xmlstarlet ed -u "//*[contains(text(),'Text')]" -v "Echo-Location" /root/nautilus.xml
Enter fullscreen mode Exit fullscreen mode
  1. Case-insensitive replacement:
   sed -i 's/Text/Echo-Location/gi' /root/nautilus.xml
Enter fullscreen mode Exit fullscreen mode

Complete Solution Summary

The task has been completed on the Jump Host Server:

  • ✅ Connected to jump host and switched to root
  • ✅ Verified /root/nautilus.xml exists
  • ✅ All occurrences of "Text" replaced with "Echo-Location"
  • ✅ Used sed -i for in-place editing
  • ✅ Verified no "Text" strings remain
  • ✅ Confirmed "Echo-Location" strings are present
  • ✅ Optional backup file created

The XML template file is now properly populated with the required string substitutions and ready for use in the Nautilus application.

Top comments (0)