在使用grep的的星座 [英] Using the star sign in grep
问题描述
我试图在Linux / bash的
I am trying to search for the substring "abc" in a specific file in linux/bash
所以,我做的:
grep '*abc*' myFile
它返回什么。
但是,如果我做的:
grep 'abc' myFile
它正确返回匹配。
It returns matches correctly.
现在,这是不是我的问题。但是,如果我想对grep更复杂的字符串,比如
Now, this is not a problem for me. But what if I want to grep for a more complex string, say
*abc * def *
我
将如何完成它使用grep?
How would I accomplish it using grep?
推荐答案
星号只是一个重复操作符,但你需要告诉它你重复一下。 / * ABC * /
匹配包含AB和零个或多个C的一个字符串(因为第二个*是放在c;第一个是没有意义的,因为没有什么让它重复) 。如果你想匹配任何东西,你需要说 *
- 点指任何字符(的中的某些准则)。如果你想只匹配ABC,你可能只是说的grep'ABC'MYFILE
。为了让您更复杂的比赛,你需要使用 *
- 的grep'ABC *高清'MYFILE
将匹配其次,它包含ABC字符串通过DEF与可选的东西之间。
The asterisk is just a repetition operator, but you need to tell it what you repeat. /*abc*/
matches a string containing ab and zero or more c's (because the second * is on the c; the first is meaningless because there's nothing for it to repeat). If you want to match anything, you need to say .*
-- the dot means any character (within certain guidelines). If you want to just match abc, you could just say grep 'abc' myFile
. For your more complex match, you need to use .*
-- grep 'abc.*def' myFile
will match a string that contains abc followed by def with something optionally in between.
基于评论更新:
*
在常规的前pression是不完全在控制台一样*。在控制台中,*是一个水珠的一部分构建,只是充当一个通配符(比如 LS * .LOG
将列出在.LOG结尾)的所有文件。然而,在常规的前pressions,*是修饰的,这意味着它仅适用于字符或组preceding它。如果你想在*定期EX pressions作为通配符,你需要使用 *
为previously提到的 - 点是通配符和明星,修改点时,意味着找到一个或多个点; IE浏览器。找到一个或多个任意字符。
*
in a regular expression is not exactly the same as * in the console. In the console, * is part of a glob construct, and just acts as a wildcard (for instance ls *.log
will list all files that end in .log). However, in regular expressions, * is a modifier, meaning that it only applies to the character or group preceding it. If you want * in regular expressions to act as a wildcard, you need to use .*
as previously mentioned -- the dot is a wildcard character, and the star, when modifying the dot, means find one or more dot; ie. find one or more of any character.
这篇关于在使用grep的的星座的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!