Sed recursively with the help of ack and xargs
You can make a sed replacement recursively with the help of ack and xargs just do the following
ack "search text" -l --print0 | xargs -0 -n 1 sed -i "s/search text/replacement text/"
This will first list the files with ack, then execute sed the expression indicated in every file, in this case the reeplace
Written by Mario Bonales
Related protips
1 Response
This is brilliant! I made it into a little script called srchrep
#!/bin/sh
replace=$1
replacement=$2
ack "$replace" -la --print0 | xargs -0 -n 1 sed -i "s/$replace/$replacement/"
or as a function for your .bashrc or .bashprofile
function srchrpc () { ack "$1" -la --print0 | xargs -0 -n 1 sed -i "s/$1/$2/"; }
- note I added the
-a
flag so that it will do the replace on all files. So, if this isn't the behaviour you are looking for, you want take care with this . - just save the script version as whatever you feel comfortable with then, make a symlink or put it in ~/bin and then you can call it like
$ srchrep searchText replacementText
- any insight is welcome and appreciated.
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Shell
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#