简体   繁体   English

grep 和 regex 存储在字符串中

[英]grep and regex stored in string

my question is quite short:我的问题很短:

a="'[0-9]*'"
grep -E '[0-9]*' #for example, line containing 000 will be recognized and printed

but

grep -E $a #line containing 000 WILL NOT be printed, why is that?

Does substitution for grep regex change the command's behaviour or have I missed something from a syntactic point of view?替换 grep regex 是否会改变命令的行为,还是从句法的角度来看我错过了什么? In other words, how do I make it so that grep accepts regex from a string stored in a variable.换句话说,我如何才能让 grep 接受来自存储在变量中的字符串的正则表达式。

Thank you in advance.先感谢您。

Quotes go around data, not in data.行情绕过去的数据,而不是数据。 That means, when you store data (in this case, a regex expression) in a variable, don't embed quotes in the variable;这意味着,当您将数据(在本例中为正则表达式)存储在变量中时,不要在变量中嵌入引号; instead, put double-quotes around the variable when you use it:相反,在使用变量时在变量周围加上双引号:

a="[0-9]*"
grep -E "$a"

You can sometimes get away with leaving the double-quotes off when using variables (as in Avinash Raj's comment), but it's not generally safe.有时您可以在使用变量时去掉双引号(如 Avinash Raj 的评论),但这通常并不安全。 In this case, it'll work fine provided there are no files or subdirectories in the current working directories with names that happen to start with a digit.在这种情况下,只要当前工作目录中没有名称恰好以数字开头的文件或子目录,它就会正常工作。 You see, without double-quotes around $a , the shell will take its value, try to split it into multiple words (not a problem here), try to expand each word that contains shell wildcards into a list of matching files (potential problem here), and pass that to the command ( grep ) as its list of arguments.你看,如果没有$a周围的双引号,shell 将取其值,尝试将其拆分为多个单词(这里不是问题),尝试将包含 shell 通配符的每个单词扩展为匹配文件列表(潜在问题)这里),并传递到命令( grep )作为它的参数列表。 That means that if you happen to have files that start with digits in the current directory, grep thinks you ran a command like this:这意味着如果您碰巧在当前目录中有以数字开头的文件, grep认为您运行了如下命令:

grep -E 1file.txt 2file.jpg 3file.etc

... and it treats the first filename as the pattern to search for, and any other filenames as files to be searched. ...并将第一个文件名视为要搜索的模式,将任何其他文件名视为要搜索的文件。 And you'll be scratching your head wondering why your script works or fails depending on which directory you happen to be in.你会挠头想知道为什么你的脚本工作或失败取决于你碰巧在哪个目录中。

Note: the pattern [0-9]* is a valid regular expression, and a valid shell glob (wildcard) pattern, but it means very different things in the two contexts.注意:模式[0-9]*是一个有效的正则表达式,也是一个有效的 shell glob(通配符)模式,但它在两种上下文中的含义非常不同。 As a regex, it means 0 or more digits in a row.作为正则表达式,它意味着一行中的 0 个或多个数字。 As a shell glob, it means something that starts with a digit.作为一个 shell glob,它意味着以数字开头的东西。 Speaking of which, grep -E '[0-9]*' is not actually going to be very useful, since everything contains strings of 0 or more digits, so it'll match every line of every file you feed it.说到这一点, grep -E '[0-9]*'实际上并不是很有用,因为所有内容都包含 0 个或多个数字的字符串,因此它会匹配您提供给它的每个文件的每一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM