3

How to search and replace recursively through multiple files in a directory, using essential tools installed in mostly any Debian/Ubuntu machine?

There are multiple answers in Stack* where to find answers to this question, such as here or here. But all are somewhat lacking in essential ways. They don't provide a correct solution except for some "easy" subset of possible inputs.

After some searching and careful study of manpages for grep, xargs and sed, this is the best "search and replace" command I've been able to build for Bash:

grep -ErlIZ -- '<OldPattern>' . | xargs -0rL1 sed -ri 's/<OldPattern>/<NewPattern>/g'

(note I want being able to use helpful and advanced shell features where possible so I'm not worried much yet about POSIX or portability -- and I don't care much either about mostly outdated versions of GNU tools in Mac)

This one-liner has multiple features:

  • Explicitly ignores binary files, for safety (not sure if this is really needed, though)
  • Uses grep | xargs to filter out candidate files and provide good performance in huge directories
  • Accepts patterns that start with a dash (-)
  • Accepts paths with spaces
  • Accepts regex capture groups in the search patterns

But due to deficiencies in the sed feature set, the regex engine is always greedy and there is no option to disable this behavior (only ugly workarounds). This means only one substitution can be done per line, at least for some cases (I can show some examples if requested).

Resorting to a while loop makes it run as many times as needed to really cover all possible substitutions:

while FILES="$(grep -ErlI -- '<OldPattern>' .)"; do
    echo "$FILES" | xargs -rL1 sed -ri 's/<OldPattern>/<NewPattern>/g'
done

But now Bash cannot store null bytes, so options grep -Z and xargs -0 had to be dropped. I believe this drops compatibility with paths that contain spaces.

  • Is it possible to combine the while loop solution with the -Z, -0 options to support paths with spaces?

  • Or maybe... is there any other, different but better way to build a robust and reliable search-and-replace command? (succinct is a feature so, as close to a one-liner as possible)


EDIT: Adding an example where the greedy regexp in sed is a problem for the non-loop version.

With this input line:

set(requires "gstreamer-1.5 gstreamer-base-1.5 gstreamer-sdp-1.5 libjsonrpc")

The pattern (gst.*)1\.5 would match this:

set(requires "[gstreamer-1.5 gstreamer-base-1.5 gstreamer-sdp-1.5] libjsonrpc")

Because it is greedy, it gets the from the first gst to the last 1.5. Say the substitution is \1AAA: the \1 will keep the (capture group), and the AAA will just print these letters instead of the original 1.5. The result will be:

set(requires "gstreamer-1.5 gstreamer-base-1.5 gstreamer-sdp-AAA libjsonrpc")

So this command would need to be run 3 times in total to actually substitute all possible matches in this line. The while loop version just runs everything once and again until the search pattern cannot be found any more, which is when the replace work has actually been finished.

  • 1
    @j1elo the null delimiter (-d '') is much more significant than the IFS= (which AFAIK only helps with edge cases, for example where the null-delimited tokens have leading or trailing whitespace) – steeldriver Feb 15 '19 at 18:47
  • 1
    bash not being able to store null bytes in a variable doesn't mean it can't send them through a pipeline. find -print0 | xargs -0 works perfectly fine. That said, this looks at the end of the day to be a sed question; the main way I work around greediness is with a smart pattern that eschews .*_ in favor of [^_]*_ (presuming you're looking for a nongreedy match up to an excluding a _). In your example case, I'd use a pattern like (gst[-a-zA-Z]*)1\.5, for instance. – DopeGhoti Feb 15 '19 at 19:08
  • 1
    Fundamentally what's confusing me here is why you believe that how you pass file arguments to sed has any effect on how a regular expression is matched within those files (greedy or otherwise) - can you give an example where that happens? – steeldriver Feb 15 '19 at 19:44
  • 2
    Regarding greediness, the only regex flavour that makes non-greediness anything close to simple is perl (or pcre): have you thought about using perl in place of sed (or in place of the whole pipeline?) – glenn jackman Feb 15 '19 at 20:11
  • 2
    Possible duplicate of Non-greedy match with SED regex (emulate perl's .*?) – DopeGhoti Feb 15 '19 at 21:56
1

If you want to run a substitution again and again as long as it succeeds, that can be done in sed with a conditional loop using the t command:

grep -ErlIZ -- '<OldPattern>' . |
   xargs -r0 sed -Ei -e :1 -e 's/<OldPattern>/<NewPattern>/g' -e t1

Here also passing as many files as possible to sed instead of running one sed per file for efficiency, and using -E which is more portable than -r outside of GNU systems (and consistent with grep -E).

bash can't store NUL in its variables but you can use an array instead to store the list of files.

With bash 4.4+:

readarray -td '' files < <(grep -ErlIZ -- '<OldPattern>' .)

Which you can then output with:

((${#files[@])) && printf '%s\0' "${files[@]}" | xargs -r0 ...

Or use a temp file. On Linux, that could be done as:

exec 3<<EOF # creates a deleted empty temp file opened on fd 3
EOF

grep -ErlIZ -- '<OldPattern>' . > /dev/fd/3 || exit

# and later:
while xargs -r0a /dev/fd/3 ...; do...

exec 3<&- # file was already deleted, closing it means its data is now
          # reclaimed.

Your (gst.*)1\.5 should probably be: (\<gst[^[:space:]]*)-1\.5\> if you want the variable part not to including whitespace character and it not to match on tagst-1.11.51 for instance.

Using non-greedy operators would probably not help much in that example. A perl-like gst.*?1.5 would still match gstreamer-1.3 foobar-1.5 in set(requires "gstreamer-1.3 foobar-1.5 gstreamer-sdp-AAA libjsonrpc")

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.