Bash – Find Replace whole line based on partial match

Using the below snippet you can find a matching string in a file and replace the entire line in the file instead of just the match.

This example should technically work cross platform with git-bash, multiple linux bash, etc. – will update post if there is a discrepancy.

#!/bin/bash

function misc_findReplace_wholeLine()
{
    VARIABLE_FIND="$1"
    VARIABLE_REPLACE="$2"
    VARIABLE_FILE="$3"

	echo "Finding: $VARIABLE_FIND"
	echo "Replacing With: $VARIABLE_REPLACE"
	echo "File to Operate On: $VARIABLE_FILE"

    sed -i "\@${VARIABLE_FIND}@c${VARIABLE_REPLACE}" "$VARIABLE_FILE"
}

#Ensure nothing happens outside the directory this script is ran from
cd "$(dirname "$0")"
SCRIPT_DIRECTORY=$(pwd)

MY_PROJECT_DIRECTORY="$SCRIPT_DIRECTORY/my-code-project"

###################################################################
#### FILE: my-code-project/my-sub-directory/file.properties  ####
###################################################################

FILE_TO_WORK_WITH="$MY_PROJECT_DIRECTORY/my-sub-directory/file.properties"

STRING_TO_FIND="host="
STRING_TO_REPLACE="host=localhost"

echo "#############################################"
echo "### Checking Before Variable Replacement: ###"
echo "#############################################"
echo ""
cat "$FILE_TO_WORK_WITH" | grep "$STRING_TO_FIND"
echo ""
echo "#############################################"


misc_findReplace_wholeLine "$STRING_TO_FIND" "$STRING_TO_REPLACE" "$FILE_TO_WORK_WITH" 

echo "############################################"
echo "### Checking After Variable Replacement: ###"
echo "############################################"
echo ""
cat "$FILE_TO_WORK_WITH" | grep "$STRING_TO_FIND"
echo ""
echo "#############################################"

Special thanks to user @stas-chernetski on Stack Overflow for providing the syntax example at the below link:

One thought on “Bash – Find Replace whole line based on partial match

  1. hey there, i have another neet trick for your script.
    getting your script’s directory is something i’ve been using a lot.
    SCRIPT_DIRECTORY=”$( cd “$( dirname “${BASH_SOURCE[0]}” )” >/dev/null 2>&1 && pwd )”
    it might seem like a terrifiying line, but it is implemented in a template of a script i have for myself.

    additionally, this line will break if your ever try to source another script which has that variable.
    what i used to solve this, is in my bash template generator i made the variable get an automatic hash in the end:
    get-hash: cat /dev/urandom | tr -dc ‘A-Z0-9’ | fold -w 16 | head -1

    SCRIPT_DIRECTORY_$(get-hash)=…

    It isn’t the cleanest solution, nor it is the best, but i guess it worked out for me for a while

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s