SlideShare a Scribd company logo
1 of 8
grep
Agenda
概述
• grep命令用于在查找指定的模式, 并且输出
  出匹配该模式的行, grep命令的输入可以是
  文件/标准输入/管道, 其中管道是指将另一
  个命令的结果作为grep命令的输入, 这很常
  见;
• grep命令衍生了很多其他的变种, 比如
  egrep/fgrep等, 这里只介绍基本的grep命令.
用法说明
• grep命令使用如下格式:
  1, grep pattern file1 file2...
  从一个或者多个文件中查找模式并且输出; 如果指定了多个文件
  则会首先输出文件名称, 然后输出匹配行.
  2, command | grep pattern
  从其他命令的输出中查找模式并打印;
  3, grep pattern
  在标准输入中查找模式并打印.
• 示例:
  grep hello textfile # 查找textfile包含hello的行
  ls | grep .*txt        # 查找当前目录下文件名以txt结尾的
                         所有文件
  grep hello             # 用户每输入一行就对该行进行匹配,
                         如果该行包含hello则打印
• 注意: 使用grep命令时, 最好使用引号将需要匹配的模式括起来.
元字符 & 命令选项
• grep命令使用的正则表达式元字符基本上与vim的相
  同, 具体可以参考vim元字符《 3, Regular Expression.pptx 》
• grep命令有如下常用选项:
  -c 仅打印匹配的行数, 不打印行的内容
  -i 模式匹配时忽略大小写
  -h 不显示文件名称
  -l 仅列出匹配文件的文件名, 每个文件仅列出一次,
      文件名之间使用换行符分隔
  -n 在输出的每一行前加上该行在文件中的行号
  -v 显示不匹配的行
退出状态
• grep命令在执行完成时, 会设置一个程序退
  出状态码, 使用$?查看. 如果找到了匹配相应
  模式的行, 状态为0; 如果没有找到则设置为
  1, 如果查找的文件不存在则设置为2. 示例:
用法示例
• 示例:
  grep ‘<Tom>’ file        # 输出包含单词Tom的行
  grep ‘Tom Savage’ file     # 输出包含Tom Savage的行
  grep ‘^Tommy’ file         # 输出以Tommy开头的行
  grep ‘.bak$’ file         # 输出以.bak结尾的行
  grep ‘[Pp]yramid’ *        # 输出当前目录所有文件包含
                             Pyramid或pyramid的行
  grep ‘[A-Z]’ file          # 输出至少包含一个大小字母的行
  grep ‘[0-9]’ file          # 输出至少包含一个数字字符的行
  grep -v ‘Mary’ file        # 输出所有不包含Mary的行
  grep -i ‘sam’ file         # 输出包含sam的行, 不区分大小写
  grep -l ‘Dear Boss’ *      # 列出所有包含Dear Boss的文件名
  grep -n ‘Tom’ file         # 输出包含Tom的行, 并加上行号
  grep “$name” file          # 展开变量name的值, 然后输出所有
  包含该值的行
  ps -ef | grep ‘^ *user1’   # 查找user1用户的进程
END

More Related Content

Viewers also liked

7, reusing classes
7, reusing classes7, reusing classes
7, reusing classested-xu
 
Handleiding Forecast Solution Shared
Handleiding Forecast Solution SharedHandleiding Forecast Solution Shared
Handleiding Forecast Solution SharedDaan Blinde
 
Copia de experiencia de aula
Copia de experiencia de aulaCopia de experiencia de aula
Copia de experiencia de aulaolguita1975
 
Informatie, Middel Voor Gezonde Sturing
Informatie, Middel Voor Gezonde SturingInformatie, Middel Voor Gezonde Sturing
Informatie, Middel Voor Gezonde SturingDaan Blinde
 
2, object oriented programming
2, object oriented programming2, object oriented programming
2, object oriented programmingted-xu
 
Master Genzyme Presentation
Master Genzyme PresentationMaster Genzyme Presentation
Master Genzyme Presentationengelske
 
три дерева - Three Trees
три дерева - Three Treesтри дерева - Three Trees
три дерева - Three TreesFreekidstories
 

Viewers also liked (14)

7, reusing classes
7, reusing classes7, reusing classes
7, reusing classes
 
Handleiding Forecast Solution Shared
Handleiding Forecast Solution SharedHandleiding Forecast Solution Shared
Handleiding Forecast Solution Shared
 
Harsh Pershad (Innovate UK) presenting 'Funding Opportunities from Innovate U...
Harsh Pershad (Innovate UK) presenting 'Funding Opportunities from Innovate U...Harsh Pershad (Innovate UK) presenting 'Funding Opportunities from Innovate U...
Harsh Pershad (Innovate UK) presenting 'Funding Opportunities from Innovate U...
 
01 Gedda Jfk2009
01 Gedda Jfk200901 Gedda Jfk2009
01 Gedda Jfk2009
 
Copia de experiencia de aula
Copia de experiencia de aulaCopia de experiencia de aula
Copia de experiencia de aula
 
Informatie, Middel Voor Gezonde Sturing
Informatie, Middel Voor Gezonde SturingInformatie, Middel Voor Gezonde Sturing
Informatie, Middel Voor Gezonde Sturing
 
2, object oriented programming
2, object oriented programming2, object oriented programming
2, object oriented programming
 
cv
cvcv
cv
 
Каталог Bravolli
Каталог BravolliКаталог Bravolli
Каталог Bravolli
 
Master Genzyme Presentation
Master Genzyme PresentationMaster Genzyme Presentation
Master Genzyme Presentation
 
Formation Gestalt IFFP 2017
Formation Gestalt IFFP 2017Formation Gestalt IFFP 2017
Formation Gestalt IFFP 2017
 
Seo in raipur
Seo in raipurSeo in raipur
Seo in raipur
 
три дерева - Three Trees
три дерева - Three Treesтри дерева - Three Trees
три дерева - Three Trees
 
Welcome To Deep Tissue Massage
Welcome To Deep Tissue MassageWelcome To Deep Tissue Massage
Welcome To Deep Tissue Massage
 

More from ted-xu

11, OCP - awr & alert system
11, OCP - awr & alert system11, OCP - awr & alert system
11, OCP - awr & alert systemted-xu
 
10, OCP - flashback
10, OCP - flashback10, OCP - flashback
10, OCP - flashbackted-xu
 
9, OCP - restore and recovery with rman
9, OCP - restore and recovery with rman9, OCP - restore and recovery with rman
9, OCP - restore and recovery with rmanted-xu
 
8, OCP - backup with rman
8, OCP - backup with rman8, OCP - backup with rman
8, OCP - backup with rmanted-xu
 
7, OCP - configure database for backup and recovery
7, OCP - configure database for backup and recovery7, OCP - configure database for backup and recovery
7, OCP - configure database for backup and recoveryted-xu
 
6, OCP - oracle security
6, OCP - oracle security6, OCP - oracle security
6, OCP - oracle securityted-xu
 
5, OCP - oracle storage
5, OCP - oracle storage5, OCP - oracle storage
5, OCP - oracle storageted-xu
 
4, OCP - oracle networking
4, OCP - oracle networking4, OCP - oracle networking
4, OCP - oracle networkingted-xu
 
3, OCP - instance management
3, OCP - instance management3, OCP - instance management
3, OCP - instance managementted-xu
 
2, OCP - installing and creating a database
2, OCP - installing and creating a database2, OCP - installing and creating a database
2, OCP - installing and creating a databaseted-xu
 
1, OCP - architecture intro
1, OCP - architecture intro1, OCP - architecture intro
1, OCP - architecture introted-xu
 
12, OCP - performance tuning
12, OCP - performance tuning12, OCP - performance tuning
12, OCP - performance tuningted-xu
 
7, business event system
7, business event system7, business event system
7, business event systemted-xu
 
6, workflow miscellaneous
6, workflow miscellaneous6, workflow miscellaneous
6, workflow miscellaneousted-xu
 
5, workflow function activity
5, workflow function activity5, workflow function activity
5, workflow function activityted-xu
 
4, workflow tables & api
4, workflow tables & api4, workflow tables & api
4, workflow tables & apited-xu
 
3, workflow in ebs
3, workflow in ebs3, workflow in ebs
3, workflow in ebsted-xu
 
2, a simple workflow
2, a simple workflow2, a simple workflow
2, a simple workflowted-xu
 
1, workflow intro
1, workflow intro1, workflow intro
1, workflow introted-xu
 
8, bes tables & api
8, bes tables & api8, bes tables & api
8, bes tables & apited-xu
 

More from ted-xu (20)

11, OCP - awr & alert system
11, OCP - awr & alert system11, OCP - awr & alert system
11, OCP - awr & alert system
 
10, OCP - flashback
10, OCP - flashback10, OCP - flashback
10, OCP - flashback
 
9, OCP - restore and recovery with rman
9, OCP - restore and recovery with rman9, OCP - restore and recovery with rman
9, OCP - restore and recovery with rman
 
8, OCP - backup with rman
8, OCP - backup with rman8, OCP - backup with rman
8, OCP - backup with rman
 
7, OCP - configure database for backup and recovery
7, OCP - configure database for backup and recovery7, OCP - configure database for backup and recovery
7, OCP - configure database for backup and recovery
 
6, OCP - oracle security
6, OCP - oracle security6, OCP - oracle security
6, OCP - oracle security
 
5, OCP - oracle storage
5, OCP - oracle storage5, OCP - oracle storage
5, OCP - oracle storage
 
4, OCP - oracle networking
4, OCP - oracle networking4, OCP - oracle networking
4, OCP - oracle networking
 
3, OCP - instance management
3, OCP - instance management3, OCP - instance management
3, OCP - instance management
 
2, OCP - installing and creating a database
2, OCP - installing and creating a database2, OCP - installing and creating a database
2, OCP - installing and creating a database
 
1, OCP - architecture intro
1, OCP - architecture intro1, OCP - architecture intro
1, OCP - architecture intro
 
12, OCP - performance tuning
12, OCP - performance tuning12, OCP - performance tuning
12, OCP - performance tuning
 
7, business event system
7, business event system7, business event system
7, business event system
 
6, workflow miscellaneous
6, workflow miscellaneous6, workflow miscellaneous
6, workflow miscellaneous
 
5, workflow function activity
5, workflow function activity5, workflow function activity
5, workflow function activity
 
4, workflow tables & api
4, workflow tables & api4, workflow tables & api
4, workflow tables & api
 
3, workflow in ebs
3, workflow in ebs3, workflow in ebs
3, workflow in ebs
 
2, a simple workflow
2, a simple workflow2, a simple workflow
2, a simple workflow
 
1, workflow intro
1, workflow intro1, workflow intro
1, workflow intro
 
8, bes tables & api
8, bes tables & api8, bes tables & api
8, bes tables & api
 

4, grep

  • 3. 概述 • grep命令用于在查找指定的模式, 并且输出 出匹配该模式的行, grep命令的输入可以是 文件/标准输入/管道, 其中管道是指将另一 个命令的结果作为grep命令的输入, 这很常 见; • grep命令衍生了很多其他的变种, 比如 egrep/fgrep等, 这里只介绍基本的grep命令.
  • 4. 用法说明 • grep命令使用如下格式: 1, grep pattern file1 file2... 从一个或者多个文件中查找模式并且输出; 如果指定了多个文件 则会首先输出文件名称, 然后输出匹配行. 2, command | grep pattern 从其他命令的输出中查找模式并打印; 3, grep pattern 在标准输入中查找模式并打印. • 示例: grep hello textfile # 查找textfile包含hello的行 ls | grep .*txt # 查找当前目录下文件名以txt结尾的 所有文件 grep hello # 用户每输入一行就对该行进行匹配, 如果该行包含hello则打印 • 注意: 使用grep命令时, 最好使用引号将需要匹配的模式括起来.
  • 5. 元字符 & 命令选项 • grep命令使用的正则表达式元字符基本上与vim的相 同, 具体可以参考vim元字符《 3, Regular Expression.pptx 》 • grep命令有如下常用选项: -c 仅打印匹配的行数, 不打印行的内容 -i 模式匹配时忽略大小写 -h 不显示文件名称 -l 仅列出匹配文件的文件名, 每个文件仅列出一次, 文件名之间使用换行符分隔 -n 在输出的每一行前加上该行在文件中的行号 -v 显示不匹配的行
  • 6. 退出状态 • grep命令在执行完成时, 会设置一个程序退 出状态码, 使用$?查看. 如果找到了匹配相应 模式的行, 状态为0; 如果没有找到则设置为 1, 如果查找的文件不存在则设置为2. 示例:
  • 7. 用法示例 • 示例: grep ‘<Tom>’ file # 输出包含单词Tom的行 grep ‘Tom Savage’ file # 输出包含Tom Savage的行 grep ‘^Tommy’ file # 输出以Tommy开头的行 grep ‘.bak$’ file # 输出以.bak结尾的行 grep ‘[Pp]yramid’ * # 输出当前目录所有文件包含 Pyramid或pyramid的行 grep ‘[A-Z]’ file # 输出至少包含一个大小字母的行 grep ‘[0-9]’ file # 输出至少包含一个数字字符的行 grep -v ‘Mary’ file # 输出所有不包含Mary的行 grep -i ‘sam’ file # 输出包含sam的行, 不区分大小写 grep -l ‘Dear Boss’ * # 列出所有包含Dear Boss的文件名 grep -n ‘Tom’ file # 输出包含Tom的行, 并加上行号 grep “$name” file # 展开变量name的值, 然后输出所有 包含该值的行 ps -ef | grep ‘^ *user1’ # 查找user1用户的进程
  • 8. END