« Back to Index

RipGrep: inline file replacements

View original Gist on GitHub

Tags: #riggrep #rg #sed #replacement #bash #shell

RipGrep inline file replacements.sh

#!/bin/bash

# DESCRIPTION:
# Replaces all instances where...
#
#   `Id` should be `ID`
#   `Acl` should be `ACL`
#   `Http` should be `HTTP`
#
# DEPENDENCIES:
# brew install ripgrep

CLIENT=$1

function replace {
  local pattern=$1
  local replacement=$2
  local file=$3

  rg $1 \
    --case-sensitive \
    --type go \
    --type md \
    --color never \
    --no-line-number \
    --passthru \
    --replace $2 \
    "$file" > tmp.txt && mv tmp.txt "$file"

  rm tmp.txt 2> /dev/null
}

FILES="./$CLIENT/**/*"
for f in $FILES
do
  echo "Processing $f..."
  # ID
  replace '\b(\w+)Id([A-Z]\w+)?\b' '${1}ID${2}' "$f"
  # ACL
  replace '([a-z])?Acl(\w+)?\b' '${1}ACL${2}' "$f"
  # HTTP
  replace '([a-z])?Http(\w+)?\b' '${1}HTTP${2}' "$f"
done

cd ./tests/simple-client-test/go-client-test
go run main.go
cd -