Skip to content

Instantly share code, notes, and snippets.

@mks-d
Last active November 17, 2019 16:35
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mks-d/1ad1de20853bac6e5ba757a15e9bbbd8 to your computer and use it in GitHub Desktop.
Recursively search & replace with GNU sed on macOS

While working on one of our Java projects on macOS it was required to do a search and replace in all Java files containing a string sequence. In our case it was required to change every JavaDoc "@since 2.3.0" into "@since 2.1.5, 2.3.0".

sed is the tool for this, and since BSD's implementation differs from GNU sed, we opted to stick to GNU sed which is available on Homebrew:

brew install gnu-sed

Then the magic combining find and sed:

find src/ -name '*.java' | xargs gsed -i -e "s/@since 2.3.0/@since 2.1.5, 2.3.0/g"

Credits to those Stack Overflow posts to make the above work like a charm:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment