每天回顾linux命令(grep)

linux下grep命令是个比较强大的命令,个人觉得每个开发者都应该学会的linux命令。在windows下,如果我们要查日志的时候会用编辑器打开后ctrl + F搜索,grep命令就是linux下的搜索工具。

grep命令
grep全称(global search regular expression(RE) and print out the line, 全局搜索正则表达式并把行打印出来),是一个强大的文本搜索工具,可以使用正则表达式搜索文本,并把匹配的行打印出来。

语法
grep (选项) (参数) 或者 grep (参数) (选项)

选项
-a:不要忽略二进制数据;grep原本是搜寻文字文件,若拿二进制的档案作为搜寻的目标,则会显示如下的讯息: Binary file 二进制文件名 matches 然后结束。若加上-a参数则可将二进制档案视为文本文件搜寻,相当于–binary-files=text这个参数。

root@68fc0a9bc6dd:/home/wzm/testgrep# grep lib64 redis-cli
Binary file redis-cli matches
root@68fc0a9bc6dd:/home/wzm/testgrep# grep -a lib64 redis-cli
 @ ( %       @       @ @     @ @     0      0                  p      p@     p@                            @       @     Lv     Lv                   �}     �}g     �}g     �&      �B                     ~      ~g      ~g     �      �                  �      �@     �@     D       D                    �}     �}g     �}g  P       P             P�td   `�     `�F     `�F     �      �             Q�td                              R�td   �}     �}g     �}g     p      p             /lib64/ld-linux-x86-64.so.2          GNU                GNU ,�]��y�b�6i  ��v+�S         |   @      A@P�    � � ^]�d��.M X � x�
root@68fc0a9bc6dd:/home/wzm/testgrep#

-A <显示列数> 除了显示复核范本样式那行之外,并显示该行之后的内容。

root@68fc0a9bc6dd:/home/wzm/testgrep# grep -A 10 wuzhiming redis.conf
#wuzhiming
# Replication SYNC strategy: disk or socket.
#
# -------------------------------------------------------
# WARNING: DISKLESS REPLICATION IS EXPERIMENTAL CURRENTLY
# -------------------------------------------------------
#
# New slaves and reconnecting slaves that are not able to continue the replication
# process just receiving differences, need to do what is called a "full
# synchronization". An RDB file is transmitted from the master to the slaves.
# The transmission can happen in two different ways:

-B:显示符合范本样式的那一行之外,并显示该行之前的内容。和-A相反

root@68fc0a9bc6dd:/home/wzm/testgrep# grep wuzhiming redis.conf -B 10
#
# Since Redis 2.6 by default slaves are read-only.
#
# Note: read only slaves are not designed to be exposed to untrusted clients
# on the internet. It's just a protection layer against misuse of the instance.
# Still a read only slave exports by default all the administrative commands
# such as CONFIG, DEBUG, and so forth. To a limited extent you can improve
# security of read only slaves using 'rename-command' to shadow all the
# administrative / dangerous commands.
slave-read-only yes
#wuzhiming

-c:计算符合范本样式的列数

root@68fc0a9bc6dd:/home/wzm/testgrep# grep wuzhiming redis.conf -c
1
root@68fc0a9bc6dd:/home/wzm/testgrep# grep redis redis.conf -c
14

-C<显示列数>:除了显示符合搜索条件的那一列之外,并显示该列

root@68fc0a9bc6dd:/home/wzm/testgrep# grep wuzhiming redis.conf -C 10
#
# Since Redis 2.6 by default slaves are read-only.
#
# Note: read only slaves are not designed to be exposed to untrusted clients
# on the internet. It's just a protection layer against misuse of the instance.
# Still a read only slave exports by default all the administrative commands
# such as CONFIG, DEBUG, and so forth. To a limited extent you can improve
# security of read only slaves using 'rename-command' to shadow all the
# administrative / dangerous commands.
slave-read-only yes
#wuzhiming
# Replication SYNC strategy: disk or socket.
#
# -------------------------------------------------------
# WARNING: DISKLESS REPLICATION IS EXPERIMENTAL CURRENTLY
# -------------------------------------------------------
#
# New slaves and reconnecting slaves that are not able to continue the replication
# process just receiving differences, need to do what is called a "full
# synchronization". An RDB file is transmitted from the master to the slaves.
# The transmission can happen in two different ways:

-d<进行动作>当指定要查找的是目录而非文件时,必须使用这项选项,否则grep命令将回报信息并停止动作。进行动作可以为read和skip

root@68fc0a9bc6dd:/home/wzm# grep wuzhiming testgrep/
grep: testgrep/: Is a directory
root@68fc0a9bc6dd:/home/wzm# grep wuzhiming testgrep/ -d "read"
grep: testgrep/: Is a directory
root@68fc0a9bc6dd:/home/wzm# grep wuzhiming testgrep/ -d "skip"
root@68fc0a9bc6dd:/home/wzm#

-e<范本样式>指定字符串作为查找文件内容的范本

root@68fc0a9bc6dd:/home/wzm/testgrep# grep -e wuzhiming -e redis redis.conf
# ./redis-server /path/to/redis.conf
# unixsocket /tmp/redis.sock
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
# is used even if not specified, defaulting to "/var/run/redis.pid".
pidfile /var/run/redis_6379.pid
# syslog-ident redis
#wuzhiming
# others with access to the host running redis-server.
# Please check http://redis.io/topics/persistence for more information.
# http://antirez.com/post/redis-persistence-demystified.html
# to fix the AOF file using the "redis-check-aof" utility before to restart
# available at http://redis.io web site.
# This feature is documented at http://redis.io/topics/notifications
#   redis-benchmark -n 1000000 incr foo
#   redis-cli object freq foo

-E:将范本样式为延伸的普通表示法来使用,意味着可以使用扩展正则表达式。

root@68fc0a9bc6dd:/home/wzm/testgrep# grep -E w.zhi redis.conf
#wuzhiming

-f<范本文件>:指定范本文件,其内容可能有一个或多个范本样式,让grep查找复核范本条件的文本内容,格式为每一列的范本样式

root@68fc0a9bc6dd:/home/wzm/testgrep# cat redis.conf |grep -f patfile.txt
# ./redis-server /path/to/redis.conf
# unixsocket /tmp/redis.sock
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
# is used even if not specified, defaulting to "/var/run/redis.pid".
pidfile /var/run/redis_6379.pid
# syslog-ident redis
#wuzhiming
# others with access to the host running redis-server.
# Please check http://redis.io/topics/persistence for more information.
# http://antirez.com/post/redis-persistence-demystified.html
# to fix the AOF file using the "redis-check-aof" utility before to restart
# available at http://redis.io web site.
# This feature is documented at http://redis.io/topics/notifications
#   redis-benchmark -n 1000000 incr foo
#   redis-cli object freq foo
root@68fc0a9bc6dd:/home/wzm/testgrep# cat patfile.txt
wu
redis

-F:将范本样式视为固定字符串的列表
-h:在显示复核范本样式的那一列之前,不标示列所属的文件名称,为默认情况。
-H:在显示复核范本样式的那一列之前,标示列所属的文件名称。

root@68fc0a9bc6dd:/home/wzm/testgrep# grep -f patfile.txt  -H redis.conf
redis.conf:# ./redis-server /path/to/redis.conf
redis.conf:# unixsocket /tmp/redis.sock
redis.conf:# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
redis.conf:# is used even if not specified, defaulting to "/var/run/redis.pid".
redis.conf:pidfile /var/run/redis_6379.pid
redis.conf:# syslog-ident redis
redis.conf:#wuzhiming
redis.conf:# others with access to the host running redis-server.
redis.conf:# Please check http://redis.io/topics/persistence for more information.
redis.conf:# http://antirez.com/post/redis-persistence-demystified.html
redis.conf:# to fix the AOF file using the "redis-check-aof" utility before to restart
redis.conf:# available at http://redis.io web site.
redis.conf:# This feature is documented at http://redis.io/topics/notifications
redis.conf:#   redis-benchmark -n 1000000 incr foo
redis.conf:#   redis-cli object freq foo

-i:忽略字符大小写的差别

root@68fc0a9bc6dd:/home/wzm/testgrep# grep WUZHIMING  -i -H redis.conf
redis.conf:#wuzhiming

-l:列出文件符合指定的范本样式的文件名称
-L:列出文件内容不符合指定范本样式的文件名称

root@68fc0a9bc6dd:/home/wzm/testgrep# grep WUZHIMING  -i -H redis.conf  redis.conf000 -l
redis.conf
root@68fc0a9bc6dd:/home/wzm/testgrep# grep WUZHIMING  -i -H redis.conf  redis.conf000 -L
redis.conf000

-n:在显示符合范本样式的那一列之前,标示出该列的编号。

root@68fc0a9bc6dd:/home/wzm/testgrep# grep WUZHIMING  -i -n redis.conf
318:#wuzhiming

-q:不显示任何信息(用来作为调试开关)

root@68fc0a9bc6dd:/home/wzm/testgrep# grep WUZHIMING  -i -n redis.conf
318:#wuzhiming
root@68fc0a9bc6dd:/home/wzm/testgrep# grep WUZHIMING  -i -n -q redis.conf

-s:不显示错误信息

root@68fc0a9bc6dd:/home/wzm# grep abc testgrep/
grep: testgrep/: Is a directory
root@68fc0a9bc6dd:/home/wzm# grep abc testgrep/ -s

-v:反向查询,输出不匹配的行。
-w:只显示全字符合的列。默认带
-x:只显示全列符合的列。

root@68fc0a9bc6dd:/home/wzm/testgrep# grep -x wuzhiming123 redis.conf
wuzhiming123
root@68fc0a9bc6dd:/home/wzm/testgrep# grep -x wuzhiming redis.conf
root@68fc0a9bc6dd:/home/wzm/testgrep#

-y:和-i相同。
-o:只输出文件中匹配到的部分。

root@68fc0a9bc6dd:/home/wzm/testgrep# grep -o -n redis redis.conf
6:redis
6:redis
109:redis
135:redis
154:redis
158:redis
178:redis
491:redis
670:redis
697:redis
761:redis
908:redis
991:redis
1216:redis
1217:redis
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值