为什么 grep 不识别右大括号?
Why grep does not recognize the right curly bracket?
我想匹配这样的字符串:
\sf{text}
我写了:
grep -rnw '.' --include \*.tex -o -e '\sf{text}
但它没有 return 任何东西。但是,如果我写
grep -rnw '.' --include \*.tex -o -e '\sf{text
它 return 是正确的文件。
为什么没有捕获右大括号?
既然你要找一个固定的字符串,你最好使用-F
而不是-e
:
grep -F '\sf{text}' files
这样你就不用担心转义字符、特殊含义等问题
来自man grep
:
Matcher Selection
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings, separated by newlines,
any of which is to be matched. (-F is specified by POSIX.)
而您使用的 -e
将字符串用作模式,因此具有特殊含义。
Matching Control
-e PATTERN, --regexp=PATTERN
Use PATTERN as the pattern. This can be used to specify multiple
search patterns, or to protect a pattern beginning with a hyphen (-).
(-e is specified by POSIX.)
更新
And how do i replace the matching pattern? in all the files that they
do contain this pattern delete it and replace it with another one.
我会使用 find
获取文件并使用 sed
替换内容。
find . -type f -exec sed -i.bak 's/\sf{text}/XXX/g' {} +
例子
$ cat a
sdi
asd \sf{text} asdf
ads
$ sed 's/\sf{text}/XXX/g' a
sdi
asd XXX asdf
ads
请注意,大括号不必转义。只是反斜杠。
我想匹配这样的字符串:
\sf{text}
我写了:
grep -rnw '.' --include \*.tex -o -e '\sf{text}
但它没有 return 任何东西。但是,如果我写
grep -rnw '.' --include \*.tex -o -e '\sf{text
它 return 是正确的文件。
为什么没有捕获右大括号?
既然你要找一个固定的字符串,你最好使用-F
而不是-e
:
grep -F '\sf{text}' files
这样你就不用担心转义字符、特殊含义等问题
来自man grep
:
Matcher Selection
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F is specified by POSIX.)
而您使用的 -e
将字符串用作模式,因此具有特殊含义。
Matching Control
-e PATTERN, --regexp=PATTERN
Use PATTERN as the pattern. This can be used to specify multiple search patterns, or to protect a pattern beginning with a hyphen (-). (-e is specified by POSIX.)
更新
And how do i replace the matching pattern? in all the files that they do contain this pattern delete it and replace it with another one.
我会使用 find
获取文件并使用 sed
替换内容。
find . -type f -exec sed -i.bak 's/\sf{text}/XXX/g' {} +
例子
$ cat a
sdi
asd \sf{text} asdf
ads
$ sed 's/\sf{text}/XXX/g' a
sdi
asd XXX asdf
ads
请注意,大括号不必转义。只是反斜杠。