击:简单的方法来传递一个" RAW"字符串用grep? [英] Bash: easy way to pass a "raw" string to grep?
问题描述
的grep
不能在命令行中使用时,美联储原始字符串,因为一些字符需要转义不被作为文字处理。例如:
grep
can't be fed "raw" strings when used from the command-line, since some characters need to be escaped to not be treated as literals. For example:
$ grep '(hello|bye)' # WON'T MATCH 'hello'
$ grep '\(hello\|bye\)' # GOOD, BUT QUICKLY BECOMES UNREADABLE
我用的printf
来自动转义字符串:
$ printf '%q' '(some|group)\n'
\(some\|group\)\\n
这产生字符串的一个bash转义版本,并使用反引号,这可以很容易地传递给grep的呼叫:
This produces a bash-escaped version of the string, and using backticks, this can easily be passed to a grep call:
$ grep `printf '%q' '(a|b|c)'`
然而,这显然不是意味着这样的:在输出一些字符都没有逃过,有些是不必要如此。例如:
However, it's clearly not meant for this: some characters in the output are not escaped, and some are unnecessarily so. For example:
$ printf '%q' '(^#)'
\(\^#\)
的 ^
传递给的grep
字符时,不应该逃脱。
The ^
character should not be escaped when passed to grep
.
是否有一个命令行工具,它原始字符串并返回,可以直接作为使用grep模式字符串的一个bash转义版本?我怎么能在纯bash中实现这一点,如果不是?
Is there a cli tool that takes a raw string and returns a bash-escaped version of the string that can be directly used as pattern with grep? How can I achieve this in pure bash, if not?
推荐答案
如果您正在试图将的grep
使用扩展的正前pression语法的方式要做到这一点是使用的grep -E
(又名 egrep的
)。你也应该知道的grep -F
(又名 fgrep一样
),并在GNU Coreutils的更新版本,的grep -P
。
If you are attempting to get grep
to use Extended Regular Expression syntax, the way to do that is to use grep -E
(aka egrep
). You should also know about grep -F
(aka fgrep
) and, in newer versions of GNU Coreutils, grep -P
.
背景:原的grep
有一个相当小套regex操作符的;这是肯·汤普逊原有的常规前pression实施。新版本具有扩展的剧目后来发展,以及兼容性的原因,有一个不同的名称。随着GNU 的grep
,只存在一个二进制文件,它明白如果援引为的grep
传统的,基本RE语法和如果调用ERE为 egrep的
。从 egrep的
在的grep
可通过使用反斜线引入特殊的意义。
Background: The original grep
had a fairly small set of regex operators; it was Ken Thompson's original regular expression implementation. A new version with an extended repertoire was developed later, and for compatibility reasons, got a different name. With GNU grep
, there is only one binary, which understands the traditional, basic RE syntax if invoked as grep
, and ERE if invoked as egrep
. Some constructs from egrep
are available in grep
by using a backslash escape to introduce special meaning.
随后,Perl编程语言,甚至进一步扩展形式主义;此正则表达式方言似乎是大多数新人错误地期待的grep
,也支持。随着的grep -P
,它;但这还没有被广泛支持的所有平台。
Subsequently, the Perl programming language has extended the formalism even further; this regex dialect seems to be what most newcomers erroneously expect grep
, too, to support. With grep -P
, it does; but this is not yet widely supported on all platforms.
所以,在的grep
,以下字符有特殊含义: ^ $ [] * \\
So, in grep
, the following characters have a special meaning: ^$[]*.\
在 egrep的
,以下字符也有特殊的意义:()| + {}
?。 (对于重复的括号是不是在原来的 egrep的
)的分组圆括号还能够与反向引用\\ 1
, \\ 2
等
In egrep
, the following characters also have a special meaning: ()|+?{}
. (The braces for repetition were not in the original egrep
.) The grouping parentheses also enable backreferences with \1
, \2
, etc.
在的grep
的很多版本,你可以放一个反斜杠<$ C之前获得 egrep的
行为$ C> egrep的特价商品。也有类似的特殊序列 \\&LT; \\方式&gt;
In many versions of grep
, you can get the egrep
behavior by putting a backslash before the egrep
specials. There are also special sequences like \<\>
.
在Perl中,一个巨大的像 \\额外的转义数w
\\ S
\\ ð
进行了介绍。在Perl 5,正则表达式设施被大大延长,与非贪婪匹配 *?
+
等,非分组括号(?:...)
,向前看符号,lookbehinds等
In Perl, a huge number of additional escapes like \w
\s
\d
were introduced. In Perl 5, the regex facility was substantially extended, with non-greedy matching *?
+?
etc, non-grouping parentheses (?:...)
, lookaheads, lookbehinds, etc.
...说了这么多,如果你真的想 egrep的
常规EX pressions转换为的grep
常规EX pressions的而不调用任何外部进程的尝试 $ {正则表达式/模式/替换}
为每个 egrep的
特殊字符;但认识到,这不处理字符类,否定字符类或反斜线正确逃逸。
... Having said that, if you really do want to convert egrep
regular expressions to grep
regular expressions without invoking any external process, try ${regex/pattern/substitution}
for each of the egrep
special characters; but recognize that this does not handle character classes, negated character classes, or backslash escapes correctly.
这篇关于击:简单的方法来传递一个&QUOT; RAW&QUOT;字符串用grep?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!