Skip to content

Instantly share code, notes, and snippets.

@jayers99
Last active March 15, 2019 19:52
Show Gist options
  • Save jayers99/f49515220c55b9db9da0fff3d69564d5 to your computer and use it in GitHub Desktop.
Save jayers99/f49515220c55b9db9da0fff3d69564d5 to your computer and use it in GitHub Desktop.
# -e regex
# -r extended regex
# -i in place replace (saves the file)
# adhoc string replace
sed -i 's/term/replace/' somefile.tf
# delete all lines containing
sed -i '/somestring/d' somefile.tf
# remove whitespace block formating
sed -e 's/ *=/ =/' somefile.tf
for i in ./*.tf; do sed -e 's/ *=/ =/'; done
# to save the file
for i in ./*.tf; do sed -i -e 's/ *=/ =/' $i; done
# missing spaces around equals
sed -r 's/([^ ])=/\1 =/' somefile.tf
for i in ./*.tf; do sed -r 's/([^ ])=/\1 =/' $i; done
for i in ./*.tf; do sed -i -r 's/([^ ])=/\1 =/' $i; done
sed -r 's/=([^ ])/= \1/' somefile.tf
for i in ./*.tf; do sed -r 's/=([^ ])/= \1/' $i; done
for i in ./*.tf; do sed -i -r 's/=([^ ])/= \1/' $i; done
# strip quotes off integer values
sed -r 's/"([0-9]+)"/\1/' somefile.tf
for i in ./*.tf; do sed -r 's/"([0-9]+)"/\1/' $i; done
for i in ./*.tf; do sed -i -r 's/"([0-9]+)"/\1/' $i; done
# reformat terraform plan
sed -r 's/: */: /' someplan.txt
cat someplan.txt | sed -r 's/: */: /' | grep -v ': <computed>'
# do this in powershell
cat someplan.txt | %{$_ -replace ": *",": "}
cat someplan.txt | %{$_ -replace ": *",": "} | Select-String -notmatch ': <computed>'
# clean up code and make it compact
sed -e 's/ *=/ =/' -e 's/^[ \t]*//' -e '/^$/d' -e '/^#/d' somefile.txt
# stats on all the policy Sids
grep -ir --no-filename --include \*.tf --exclude-dir=.terraform --exclude-dir=.git '"sid"' | sed -e 's/^\s*\"[S|s]id\"\s*\:\s*//' | sort | uniq -c | sort -nr
# replace all the terraform config provider versions
repo_dir=$(git rev-parse --show-toplevel)
vpc_dirs=$(ls -d $repo_dir/vpc-*)
for i in $vpc_dirs; do echo $i; sed -i -e 's/version = \"~> [0-9]\.[0-9]*\"/version = \"~> 1.58\"/g' $i/config.tf; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment