Skip to content

Instantly share code, notes, and snippets.

@mec
Created January 23, 2020 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mec/6351a25277f06bc79f5b1521b81d494b to your computer and use it in GitHub Desktop.
Save mec/6351a25277f06bc79f5b1521b81d494b to your computer and use it in GitHub Desktop.
ack and sed to find and replace accross a project
ack <search term> -l --print0 | xargs -0 sed -i '' 's/<search term>/<replacement term>/g'
  • ack takes a search term, and with the -l flag, it outputs only filenames
  • --print0 separates filenames with a null byte instead of a newline. This is nice for filenames that contain spaces (non programmers!)- We take the resulting list and pipe it to xargs which will iterate over each listing and applies the following command. Note the -0 flag so that it knows to look for the null byte character
  • Next comes sed. The OS X implementation requires an extension following the -i flag for the in place edit functionality. We pass it an empty string ('') to tell it not to use an extension
  • Finally, we do our sed function s/regular expression/replacement/flags. s is for substitute, and /g at the end means to make the substitutions for all matches

Source: https://coderwall.com/p/7ol_ja/os-x-and-project-wide-find-and-replace

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