grep 去掉 grep
Grep or Global Regular Expression Print is used to search for text or patterns in a Linux system. It can search in files, directories, and even outputs of other commands.
Grep或全局正则表达式打印用于在Linux系统中搜索文本或模式。 它可以搜索文件,目录,甚至其他命令的输出。
Regular expressions are patterns that can match text according to user’s needs. These are like rules for pattern matching.
正则表达式是可以根据用户需求匹配文本的模式。 这些就像模式匹配的规则。
Grep is often used along with regular expressions to search for patterns in text. Let’s see some practical examples of regex with grep.
Grep通常与正则表达式一起使用以搜索文本中的模式。 让我们来看一些使用grep的正则表达式的实际示例。
1.匹配单词,不论大小写 (1. Matching a word irrespective of its case)
Sometimes in a text, the same word can be written in different ways. This is most commonly the case with proper nouns. Instead of starting with an uppercase letter, sometimes they are written in all lowercase letters.
有时在文本中,可以用不同的方式写相同的单词。 最常见的情况就是专有名词。 有时不是以大写字母开头,而是全部以小写字母书写。
$ grep "[Jj]ayant"
Both the versions of the word, irrespective of their case have been matched.
不论大小写,该词的两个版本均已匹配。
Another interesting case can be observed with the word ‘IoT’. A word like this might occur several times across the text with different variations. to match all the words irrespective of the case use :
可以用“ IoT”一词观察到另一个有趣的情况。 像这样的单词可能在文本中以不同的形式出现多次。 匹配所有单词,不论大小写使用:
$ grep "[iI][oO][tT]"
2.使用带有grep的正则表达式匹配手机号码 (2. Matching mobile number using regex with grep)
Regular expressions can be used to extract mobile number from a text.
正则表达式可用于从文本中提取手机号码。
The format of the mobile number has to be known beforehand. For example, a regular expression designed to match mobile numbers won’t work for home telephone numbers.
手机号码的格式必须事先知道。 例如,旨在匹配移动电话号码的正则表达式不适用于家庭电话号码。
In this example, mobile number