浙江省高等学校教师教育理论培训

微信搜索“毛凌志岗前心得”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

linux grep 使用方法 « Neo's notes

linux grep 使用方法

 

1.在一个文件内搜索某个字符串

grep 'match_word' file

2.在多个文件内搜索某个字符串

grep 'match_word' file file2

grep 'match_word' *.rb      # 后面可以跟正则\

3.不区分大小写

grep -i 'match_word' file

4.正则表达式

grep 'regular' file  # 放一个正则表达式也是ok的

5.全字匹配

grep -w 'the' file   # 会匹配the,但是不会匹配there

6.匹配前后几行(match with A/B/C)

grep -A 3 'the' file # 匹配出现the的那行并且也输出后面的三行

grep -B 3 'the' file # 匹配出现the的那行并且也输出前面的三行

grep -C 3 'the' file # 匹配出现the的那行并且也输出前后三行

7.搜索目录下的文件

grep -r 'the' ./  # 搜索目录下面的文件里面报行the的文件

grep -R 'the' ./  # 递归搜索

8.反向匹配

grep -v 'the' file # 找不不含有the的行

9.反向匹配多个字符

grep -v -e 'the' -e 'this' -e 'find' # 匹配不包含the、this、find的行

10.找到匹配的数量

grep -c 'the' file # 找出字符the的行数的数量

11.显示匹配的的行号

grep -n 'the' file # 找出file内匹配the的行,并且显示行号

12.仅仅显示匹配的文件名称

grep -l 'the' *.rb  # 所有包含字符the的文件名称

13.仅仅显示匹配的字符串

grep -o 'the' file  #  仅仅显示每行的the

14.多颜色显示

grep -v 'the' file --color

或者

export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8' ;

grep 'the' file
posted on 2012-08-01 20:51  lexus  阅读(228)  评论(0编辑  收藏  举报