Find and Replace with Sed: Rename Golang Packages

I just consolidated my GitHub, Twitter and Instagram handle. They are the same as the .com domain I bought for this blog. As I renamed my GitHub handle, I also needed to fix import paths and rename golang packages hosted there.

I used Sed, everybody’s (and also their parents’ and their grandparents’) favourite stream editor. It’s from 1974 – the same year Judas Priest released their debut album, Rocka Rolla. Because Sed is available on all platforms and works directly with text files, you could re-use this recipe outside golang.

The syntax to find and replace strings recursively across files in the current directory looks like this:

sed -i 's/github.com/oldname//github.com/newname//g' **/*.go

Super simple eh? All you need to remember is the following:

  • The flag -i asks for an in-place edit (i.e, replace content in the files)
  • The operator s/ asks for a substitution.
  • The /g at the en informs Sed that the substitution is global – it is to be done for all matching occurrences.
  • The original package name and the new package name come between the s/ and the /g, separated by a forward slash /.
  • We need to use back-slashes to escape regular slashes that occur within the text we’re searching for and the text that is being replaced (I had to replace github.com/oldname/ with github.com/newname/)
  • **/*.go is used to recursively hunt down all .go files from the directory we are operating out of.

I suggest doing these kind of operations only after you have checked in code into GitHub so that if the worst happens you can restore a working copy from there. Always do a git diff between the old version and the new version to see what has changed. You could also use Meld if you want to compare directories that contain multiple git repositories:

meld oldfolder newfolder

There’s a lot more to sed than simple substitutions; so head over to a more in-depth tutorial like https://opensourceforu.com/2011/04/sed-explained-part-1/ or https://www.tutorialspoint.com/unix/unix-regular-expressions.htm to learn more!

Published by

WingedRhino

Because unicorns aren't real

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s