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 which is in the following format: 91-1234567890 (i.e TwoDigit-TenDigit) will be matched.

在此示例中,将采用以下格式的手机号码:91-1234567890(即TwoDigit-TenDigit)。


$ grep "[[:digit:]]\{2\}[ -]\?[[:digit:]]\{10\}"

As is evident, only the mobile number in the above-mentioned format is matched.

显然,只有上述格式的手机号码是匹配的。

3.匹配电子邮件地址 (3. Match email-address )

Extracting email address out of a text is very useful and can be achieved using grep.

从文本中提取电子邮件地址非常有用,可以使用grep实现。

An email address has a particular format. The part before the ‘@’ is the username that identifies the mailbox. Then there is a domain like gmail.com or yahoo.in.

电子邮件地址具有特定格式。 “ @”之前的部分是用于标识邮箱的用户名。 然后是一个域名,例如gmail.com或yahoo.in。

The regular expression can be designed keeping these things in mind.

可以设计正则表达式来牢记这些内容。


$ grep -E "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}"
Input File For Email
电子邮件输入文件
grep command on input.txt
input.txt上的grep命令
  • [A-Za-z0-9._%+-]+ captures the username before ‘@’[A-Za-z0-9 ._%+-] +捕获“ @”之前的用户名
  • [A-Za-z0-9.-]+ captures the name of the domain without the ‘.com’ part[A-Za-z0-9 .-] +捕获没有“ .com”部分的域名
  • .[A-Za-z]{2,6} captures the ‘.com’ or ‘.in’ etc.。[A-Za-z] {2,6}捕获“ .com”或“ .in”等。

4. URL检查器 (4. URL checker )

A URL has a particular format of representation. A regex can be built that verifies if a URL is in proper form or not.

URL具有特定的表示格式。 可以构建一个正则表达式来验证URL是否采用正确的格式。

A URL must start with http/https/ftp followed by ‘://’. Then there is the domain name which can end with ‘.com’, ‘.in’, ‘.org’ etc.

URL必须以http / https / ftp开头,后跟“://”。 然后是可以以“ .com”,“。in”,“。org”等结尾的域名。


$ grep -E "^(http|https|ftp):[\/]{2}([a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,4})"
Input Text For domain.txt
域.txt的输入文本
Grep On domain.txt
Grep在domain.txt上

-E used in this example and the previous signifies extended grep which uses Extended Regular Expression set instead of Basic Regular Expression set. This means that certain special characters are not required to be escaped. It makes the process of writing a complex regex less tiresome. Read more about it here.

在本示例和前一个示例中使用的-E表示扩展grep,它使用扩展正则表达式集而不是基本正则表达式集。 这意味着某些特殊字符不需要转义。 它使编写复杂的正则表达式的过程更加轻松。 在此处了解更多信息。

5.查找具有特定扩展名的文件 (5. Finding files with a particular extension )

ls command displays all the files in current directory.

ls命令显示当前目录中的所有文件。

running ls -l gives extra information regarding the files. Grep can be used along with ls -l command to match pattern in its output.

运行ls -l可提供有关文件的更多信息。 Grep可以与ls -l命令一起使用,以匹配其输出中的模式。

To grab files that are saved with the extension ‘.txt’ use:

要获取以扩展名“ .txt”保存的文件,请使用:


$ ls -l | grep '.txt$'

6.在括号内查找内容 (6. Find content within parenthesis )

Often text files have contents within a parenthesis. We can extract these using regex with grep.

文本文件通常在括号内包含内容。 我们可以使用带有grep的正则表达式来提取它们。


$ grep "([A-Za-z ]*)"

The regex will pick the text that’s within parathesis. The length of the content within parenthesis can also be specified.

正则表达式将选择位于括号内的文本。 也可以指定括号内内容的长度。

For example, To match parenthesis with only 10 characters use :

例如,要仅用10个字符匹配括号,请使用:


$ grep "([A-Za-z ]{10})"

7.匹配以特定单词开头的行 (7. Match lines starting with a particular word )

We can use regex to find lines that start with a particular word.

我们可以使用正则表达式来查找以特定单词开头的行。

Contents of input.txt
input.txt的内容

To find lines starting with the word Apples use :

要查找以苹果这个词开头的行,请使用:


grep '^Apples' input.txt

Similarly, lines starting with any other word can also be found.

同样,也可以找到以任何其他单词开头的行。

We can match lines ending with a specific word using the below regular expressions.

我们可以使用以下正则表达式匹配以特定单词结尾的行。


$ grep 'apples.$' input.txt

8.一次匹配多个单词 (8. Matching multiple words at once )

Let’s match multiple words with regex as shown below:

让我们用正则表达式匹配多个单词,如下所示:


$ grep 'Apples\|Orange' input.txt

This command works line an OR between the two words. It matches lines that contain either of the two words.

此命令在两个字之间的OR处起作用。 它匹配包含两个单词之一的行。

To do an AND between the two words use:

要在两个词之间进行“与”运算,请使用:


$ grep 'Apple' input.txt | grep 'Orange

9.以不同形式匹配相同的单词 (9. Matching the same word in different forms )

Sometimes a word can occur in different forms. They can differ based on the tense they’re used in.

有时,单词可能以不同的形式出现。 它们可能会根据使用时态而有所不同。

Peeled and Peeling are examples of this. In both the words, the root word is ‘peel’

去皮和去皮就是这样的例子。 在这两个词中,词根均是“果皮”

We can use regex to match all forms of a word.

我们可以使用正则表达式来匹配单词的所有形式。

In our text, we have spelled peeled and peeling as pealed and pealing respectively.

在我们的文字中,我们分别将剥皮和剥皮拼写为剥皮和剥皮。

We can also translate from US English to UK English in a similar way. For example, the word color becomes colour.

我们也可以通过类似的方式将美国英语翻译为英国英语。 例如,颜色一词变成颜色。


$ grep 'peal\([a-z]*\)\(\.*[[:space:]]\)' input.txt

10.在/ etc / passwd文件中找到用户 (10. Fiding users in /etc/passwd file)

grep can be used to get users from the /etc/passwd/ file. The /etc/passwd file maintains the list of the users on the system along with some additional information.

grep可用于从/ etc / passwd /文件获取用户。 / etc / passwd文件维护系统上的用户列表以及一些其他信息。


$ grep "Adam" /etc/passwd

The command uses grep on a system file. When the word “Adam” is found, we can see the line as output. We can perform the same search for any other element in the file.

该命令在系统文件上使用grep。 找到单词“ Adam”后,我们可以看到该行作为输出。 我们可以对文件中的任何其他元素执行相同的搜索。

结论 (Conclusion )

Regex along with grep command can be very powerful. Regex is studied as a separate field in computer science and can be used to match highly complex patterns. Learn more about regex here.

正则表达式以及grep命令可以非常强大。 正则表达式是计算机科学中的一个独立领域,可用于匹配高度复杂的模式。 在此处了解有关正则表达式的更多信息。

翻译自: https://www.journaldev.com/41403/regex-with-grep

grep 去掉 grep

grep 去掉 grep_使用grep的regex的10个实用示例相关推荐

  1. 强大的grep用法详解:grep与正则表达式

    from:http://hi.baidu.com/nearlove/blog/item/11db98b6b5b8aff831add1e5.html 首先要记住的是: 正则表达式与通配符不一样,它们表示 ...

  2. grep命令详解(如何匹配多个字符串)(grep指令)(grep -q)

    文章目录 20221011 grep如何匹配多个标志? 20230412 grep -q(`Use grep -q instead of comparing output with [ -n .. ] ...

  3. Linux中使用grep -v grep 查找不含有 grep 字段的行

    1.在Linux操作时,有时不知道进程id号是多少,使用下面命令 ps -ef |grep java 此时也会查找到含有grep的行 2.使用grep -v grep 查找不含有 grep 字段的行 ...

  4. 六周第一次课(1月15日) 9.1 正则介绍_grep上 9.2 grep中 9.3 grep下

    六周第一次课(1月15日) 9.1 正则介绍_grep上 9.2 grep中 9.3 grep下 在计算机科学中,对"正则表达式" 的定义是:它使用单个字符串来描述或匹配一系列符合 ...

  5. linux grep -a命令,linux grep用法

    ◎参数 1. -A NUM,--after-context=NUM 除了列出符合行之外,并且列出后NUM行. ex:   $ grep -A 1 panda file (从file中搜寻有panda样 ...

  6. grep 命令系列:grep 中的正则表达式

    grep 命令系列:grep 中的正则表达式 在 Linux .类 Unix 系统中我该如何使用 Grep 命令的正则表达式呢? Linux 附带有 GNU grep 命令工具,它支持扩展正则表达式e ...

  7. grep用法详解:grep与正则表达式

    首先要记住的是: 正则表达式与通配符不一样,它们表示的含义并不相同!正则表达式只是一种表示法,只要工具支持这种表示法, 那么该工具就可以处理正则表达式的字符串.vim.grep.awk .sed 都支 ...

  8. linux grep 正则 id,Linux grep正则表达式

    正则表达式只是字符串的一种描述,只有和支持正则表达式的工具相结合才能进行字符串处理.本文以grep为例来讲解正则表达式. grep一般情况下支持基本正则表达式,可以通过参数-E支持扩展正则表达式,另外 ...

  9. linux tail grep多个,Linux grep、tail命令的混合使用

    grep Linux grep命令用于查找文件里符合条件的字符串. grep指令用于查找内容包含指定的范本样式的文件,如果发现某文件的内容符合所指定的范本样式,预设grep指令会把含有范本样式的那一列 ...

最新文章

  1. 从算法+数据结构到MVC
  2. matlab绘制频散曲线,Matlab绘制频散曲线程序代码.docx
  3. IA32中栈帧结构图
  4. uni-app——一种通过Nginx反向代理处理302重定向请求解决网络请求中无法获取Cookie的解决方案
  5. lvs dr模式安装
  6. CCF202109-2 非零段划分
  7. Hash类的键值对允不允许为空的问题
  8. C11标准库原子操作/无锁队列 stdatomic.h
  9. 已解决——pycharm在同目录下import,pycharm会提示错误,但是可以运行
  10. Win10 家庭版找不到 gpedit.msc;win10怎样关闭windows defender
  11. 常见排序算法的时间复杂度汇总
  12. SpringBoot+Vue实现前后端分离的实习管理系统
  13. 苹果iPhone手机用iTunes更新IOS14.3系统失败怎么解决
  14. 实验一:线性表的基本操作实现及其应用
  15. 计算机与或非异或符号,2.6 与非、或非及异或门(1)
  16. 微信小程序-从0到1实现小程序内打开H5链接或跳转到某个公众号文章
  17. 彩信文件服务器,彩信服务器怎么设置
  18. Deep Voice 论文
  19. IPAD/IOS 访问限制密码找回方法(设置-通用-访问限制-密码的非刷机找回方法)
  20. CE扫雷20211031

热门文章

  1. Centos如何通过yum安装php7
  2. 葡萄城报表介绍:数据钻取
  3. Caffe 议事(一):从零开始搭建 ResNet 之 残差网络结构介绍和数据准备
  4. 【BZOJ 1052】 1052: [HAOI2007]覆盖问题 (乱搞)
  5. HTML+CSS 整站 步骤
  6. iOS开发学习笔记二:UITableView(1)
  7. 【已解决】python远程连接数据库问题
  8. [转载] Numpy 使用教程--Numpy 数学函数及代数运算
  9. I/O接口标准(1):LVTTL、LVCMOS、SSTL、HSTL
  10. python @的作用