bash 带通配符的 grep

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30012732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:54:56  来源:igfitidea点击:

grep with wildcard symbols

bashgrepwildcard

提问by theG

Wildcard search with grep

使用 grep 进行通配符搜索

I have a file that contains many IP addresses. I wanted to list all the ip addresses in the file and I used grep with a pattern 192.16* but it doesn't show the whole list of IP addresses. I am able to list the whole IP addresses only while using period followed with an asterisk symbol. So my doubt is Why 2nd option not working but 3rd option works fine.

我有一个包含许多 IP 地址的文件。我想列出文件中的所有 ip 地址,我使用了模式为 192.16* 的 grep,但它没有显示整个 IP 地址列表。我只能在使用句点后跟星号符号时列出整个 IP 地址。所以我的疑问是为什么第二个选项不起作用但第三个选项工作正常。

root@test:~/test# cat z
192.168.1.0
192.168.2.0
192.168.110.7
192.168.115.5

1. root@test:~/test# grep -o 192.1 z
192.1
192.1
192.1
192.1


2. root@test:~/test# grep -o 192.1* z
192.1
192.1
192.1
192.1

3. root@test:~/test# grep -o 192.1. z
192.16
192.16
192.16
192.16


4. root@test:~/test# grep -o 192.1.* z
192.168.1.0
192.168.2.0
192.168.110.7
192.168.115.5

回答by Karoly Horvath

  • A dot (.) matches anycharacter, you have to escape it: \..

  • -oshows only the matching part, if you ommit .*(= any characters) from the end, it will ommit the rest of the line (as it's not part of the matched string).

  • .*can match a lot more than you need (it will match the rest of the line), prefer to say explicitly what you allow: [0-9.]*.

  • Make sure you put the search expression in single quotes '192.168\.[0-9.]*', otherwise the shell will interpret the special characters, and substitute the expression with the matched filenames (luckily you didn't have any matching filenames).

  • You might only want to searh for words (-w). If you want to make sure you only match IP addresses and not something that resembles to it (no consecutive dots, exactly 4 digits, <=255...) then you'll need a lot more complex expression.

  • 点 ( .) 匹配任何字符,您必须对其进行转义:\.

  • -o仅显示匹配部分,如果.*从末尾省略(= 任何字符),它将省略该行的其余部分(因为它不是匹配字符串的一部分)。

  • .*可以匹配比您需要的更多(它将匹配该行的其余部分),更愿意明确说明您允许的内容:[0-9.]*

  • 确保将搜索表达式放在单引号中'192.168\.[0-9.]*',否则 shell 将解释特殊字符,并用匹配的文件名替换表达式(幸运的是您没有任何匹配的文件名)。

  • 您可能只想搜索单词 ( -w)。如果你想确保你只匹配 IP 地址而不是类似的东西(没有连续的点,正好是 4 位数字,<=255...),那么你需要更复杂的表达式。

回答by Michael Jaros

Why your commands are (not) working:

为什么你的命令(不)工作:

1. root@test:~/test# grep -o 192.1 z

Only 192<any char>1will be matched, and only the matching part will be printed because of the -oswitch.

只有192<any char>1会被匹配,因为-o切换,只会打印匹配的部分。

2. root@test:~/test# grep -o 192.1* z

Only 192<any char>, 192<any char>1, 192<any char>11, 192<any char>111etc. will be matched, and only the matching part will be printed because of the -oswitch. Your input does not contain data where this makes any difference.

192<any char>192<any char>1192<any char>11192<any char>111等等将被匹配,并且因为只有匹配的部分将被打印-o开关。您的输入不包含有任何区别的数据。

3. root@test:~/test# grep -o 192.1. z

Only 192<any char>1<any char>will be matched, and only the matching part will be printed because of the -oswitch. This gives you one more character (.stands for "any" single character).

只有192<any char>1<any char>会被匹配,因为-o切换,只会打印匹配的部分。这为您提供了一个字符(.代表“任何”单个字符)。

4. root@test:~/test# grep -o 192.1.* z

Any line starting with 192<any char>1will be matched, and only the matching part will be printed because of the -oswitch. .*matches anything up to the end of the line, including the empty string.

任何以开头的行192<any char>1都会被匹配,因为-o切换,只会打印匹配的部分。.*匹配行尾的任何内容,包括空字符串。

Regular expression for IP addresses

IP 地址的正则表达式

You can find lots of IP address regular expressions on the web, see for example this StackOverflow question. Note however that some of the expressions are used to match only the IP address and therefore contain beginning- (^) and end-of-line ($) characters. You will have to remove those if your input contains more than just the addresses.

您可以在网上找到很多 IP 地址正则表达式,例如参见这个StackOverflow 问题。但是请注意,某些表达式仅用于匹配 IP 地址,因此包含开头 ( ^) 和行尾 ( $) 字符。如果您的输入包含的不仅仅是地址,您将不得不删除它们。