« Back to Index

sed replace content across multiple files

View original Gist on GitHub

Tags: #shell

sed replace content across multiple files.bash

gsed -i 's/metrics.NewWriter(/metrics.NewLegacyWriter(/' **/*.go

# UPDATE: glob/wildcard doesn't appear to work any more, needs to be individual file streams

find . -type f -name *.go -exec gsed -i 's/foo/bar/' {} \;

# You'll need both -e (extended regex support) and -r (allow backreferences like \1 \2 etc) for more complex examples:

find . -type f -name *.go -exec gsed -i -r -e 's/, ErrMissing(.+)/, NewFieldError("\1")/' {} \;
find . -type f -name *.go -exec gsed -i -r -e 's/(github.com\/)fastly(\/customcli\/)/\1integralist\2/' {} \;

vim equivalent.viml

:argdo %s/metrics.NewWriter(/metrics.NewLegacyWriter(/ge | update

# If I'm working with a file type (like go) that messes with the quickfix window I'll typically pipe the list of files into Vim with no vimrc set.

vim initial search.bash

vim $(search "--files=*.go" "metrics\.NewWriter" ./ | cut -d ':' -f 1)