Skip to content

Instantly share code, notes, and snippets.

@linknum23
Last active October 14, 2019 20:14
Show Gist options
  • Save linknum23/1ee47cf68715f8ba0721864cfe17889e to your computer and use it in GitHub Desktop.
Save linknum23/1ee47cf68715f8ba0721864cfe17889e to your computer and use it in GitHub Desktop.
Advanced find and replace/prepend with sed
#!/bin/bash
# these ideas was used during conversion from C code to C++
directory_whitelist="dir1 dir2" # directories to replace files in
for dir in $(directory_whitelist); do
pushd $dir;
# do replacement but use prefix to make sure replacement only happens once (in this case prepending std::)
# \1 in the replace portion of the sed command is used to keep the character before the match (typically a space or '(')
# otherwise that character will get overwritten with the replacement
grep -rl --include={*.h,*.cpp} "[^std::]abs(" | xargs -i@ sed -i 's/\([^std::]\)abs(/\1std::abs(/g' @;
popd;
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment