天天看點

每天回顧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
 @ ( %       @       @ @     @ @                             p      [email protected]     [email protected]                            @       @     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-.so.          GNU                GNU ,�]��y�b�i  ��v+�S         |   @      A@P�    � � ^]�d��.M X � x�
root@68fc0a9bc6dd:/home/wzm/testgrep#
           

-A <顯示列數> 除了顯示複核範本樣式那行之外,并顯示該行之後的内容。

[email protected]:/home/wzm/testgrep# grep -A  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相反

[email protected]:/home/wzm/testgrep# grep wuzhiming redis.conf -B 
#
# 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

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

           

-C<顯示列數>:除了顯示符合搜尋條件的那一列之外,并顯示該列

[email protected]:/home/wzm/testgrep# grep wuzhiming redis.conf -C 
#
# 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<範本樣式>指定字元串作為查找檔案内容的範本

[email protected]:/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.conf00
           

-n:在顯示符合範本樣式的那一列之前,标示出該列的編号。

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

-q:不顯示任何資訊(用來作為調試開關)

root@68fc0a9bc6dd:/home/wzm/testgrep# grep WUZHIMING  -i -n redis.conf
:#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
:redis
:redis
:redis
:redis
:redis
:redis
:redis
:redis
:redis
:redis
:redis
:redis
:redis
:redis
:redis
           

繼續閱讀