"sed" provides a method for searching-and-replacing a text in a specified file in bash, so you can make some scripts which can replace necessary things easily.

Example 1: Replace file with the 'sed' command

#!/bin/bash

# Assign the filename
filename="Sales.txt"

# Take the search string
read -p "Enter the search string: " search

# Take the replace string
read -p "Enter the replace string: " replace

if [[ $search != "" && $replace != "" ]]; then
sed -i "s/$search/$replace/" $filename
fi


Example 2: Replace file with the 'sed' command with 'g' and 'i' flag - 'g' means searching globally and 'i' means case-insensitive.

#!/bin/bash

# Take the search string
read -p "Enter the search string: " search

# Take the replace string
read -p "Enter the replace string: " replace

if [[ $search != "" && $replace != "" ]]; then
sed -i "s/$search/$replace/gi" $1
fi


Example 3: Replace file with 'sed' command and matching digit pattern

The following script will search for all numerical content in a file and will replace the content by adding the ‘$’ symbol at the beginning of the numbers.

#!/bin/bash

# Check the command line argument value exists or not
if [ $1 != "" ]; then
# Search all string containing digits and add $
sed -i 's/\b[0-9]\{5\}\b/$&/g' $1
fi