Last active
August 31, 2019 05:22
-
-
Save kates/5915285 to your computer and use it in GitHub Desktop.
bulk search and replace with the silver searcher, awk, sed and xargs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ag "sometext" --nogroup | awk '{print substr($1,1,index($1,":")-1);}' | xargs -I {} sed -i .bak -e 's/sometext/anothertext/g' {} |
Yes. The string passed to sed is single quoted so it shouldn't be an issue. The script will just be a little harder to read.
ag "$1" --nogroup | awk '{print substr($1,1,index($1,":")-1);}' | xargs -I {} sed -i '.back' -e "s/$1/$2/g" {}
Here's a small improvement:
ag $1 --files-with-matches | xargs -I {} sed -i '.back' -e "s/$1/$2/g" {}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is it possible to make it into a script with arguments?
The default argument names are conflicting with the awk commands...