You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

22 lines
297 B

2 years ago
  1. #!/bin/bash
  2. usage() {
  3. echo "Usage: $(basename "$0") <REGEX> <STRING> <FILE>"
  4. }
  5. if [[ $# -ne 3 ]]; then
  6. usage
  7. exit 2
  8. fi
  9. REGEX=$1
  10. STRING=$2
  11. FILE=$3
  12. if ! grep -Eq "$REGEX" "$FILE"; then
  13. printf "%s\n" "$STRING" >>$FILE
  14. echo appended
  15. else
  16. sed -Ei "s|$REGEX|$STRING|g" $FILE
  17. echo replaced
  18. fi