#!/bin/bash usage() { echo "Usage: $(basename "$0") " } if [[ $# -ne 3 ]]; then usage exit 2 fi REGEX=$1 STRING=$2 FILE=$3 if ! grep -Pq "$REGEX" "$FILE"; then printf "$STRING\n" >>$FILE echo appended else sed -i "s|$REGEX|$STRING|g" $FILE echo replaced fi