问题
I have to replace following String
//@Config(manifest
with below string,
@Config(manifest
So this i created following regex
\/\/@Config\(manifest
And tried
grep -rl \/\/@Config\(manifest . | xargs sed -i "\/\/@Config\(manifest@Config\(manifest/g"
But i am getting following error:
sed: -e expression #1, char 38: Unmatched ( or \(
I have to search recursively and do this operation, though i am stuck with above error.
回答1:
grep -rl '//@Config(manifest' | xargs sed -i 's|//@Config(manifest|@Config(manifest|g'
- Specifying
.for current directory is optional forgrep -r sedallows Any character other than backslash or newline to be used as delimiter
Edit
If file name contains spaces, use
grep -rlZ '//@Config(manifest' | xargs -0 sed -i 's|//@Config(manifest|@Config(manifest|g'
Explanation (assumes GNU version of commands)
grep
-rperforms recursive search-loption outputs only filenames instead of matched patterns-Zoutputs a zero byte (ASCII NUL character) after each file name instead of usual newline'pattern'by default, grep uses BRE (basic regular expression) where characters like(do not have special meaning and hence need not be escaped
xargs -0tellsxargsto separate arguments by the ASCII NUL charactersed
-iinplace edit, use-i.bkpif you want to create backup of original filess|pattern|replace|gthegflag tellssedto search and replace all occurrences.sedalso defaults to BRE and so no need to escape(. Using\(would mean start of capture groups and hence the error when it doesn't find the closing\)
来源:https://stackoverflow.com/questions/39218941/search-and-replace-string-from-text-file-ubuntu