grep -lr ABCSOMETHING . | xargs sed -i 's/ABCSOMETHING/SOMETHING/g'... but sometimes you can get an error like:
sed: can't read ./Next: No such file or directoryIssuing solely the grep part reveals that spaces are the culprit:
grep -lr ABCSOMETHING .
./Next version/some file.xml
./Next version/another file.xml
./Next version/another file.xml
The solution is to change the delimiter upon which the 2 piped commands communicate to a null character:
grep -lrZ ABCSOMETHING . | xargs -0 sed -i 's/ABCSOMETHING/SOMETHING/g'
The relevant quote from xargs manual:
--null-0 Input items are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literally). Disables the end of file string, which is treated like any other argument. Useful when input items might contain white space, quote marks, or backslashes. The GNU find -print0 option produces input suitable for this mode.
And the same from grep manual:
-Z, --nullOutput a zero byte (the ASCII NUL character) instead of the character that normally follows a file name. For example, grep -lZ outputs a zero byte after each file name instead of the usual newline. This option makes the output unambiguous, even in the presence of file names containing unusual characters like newlines. This option can be used with commands like find -print0, perl -0, sort -z, and xargs -0 to process arbitrary file names, even those that contain newline characters.
Brak komentarzy:
Prześlij komentarz