首頁 > 軟體

Linux如何搜尋查詢檔案裡面內容

2020-06-16 17:35:01

在Linux系統當中,如何搜、索查詢檔案裡面的內容呢? 這個應該是系統維護、管理當中遇到最常見的需求。那麼下面介紹,總結一下如何搜尋、查詢檔案當中的內容。

搜尋、查詢檔案當中的內容,一般最常用的是grep命令,另外還有egrep, vi命令也能搜尋檔案裡面內容

1:搜尋某個檔案裡面是否包含字串,使用grep "search content" filename1, 例如

$ grep ORA alert_gsp.log

$ grep "ORA" alert_gsp.log

例如我們需要搜尋、查詢utlspadv.sql檔案中包含ORA的字元內容

[Oracle@DB-Server admin]$ grep "ORA" utlspadv.sql
  --   ORA-XXXXX:        Monitoring already started. If for example you want 
  --   ORA-20111:
  --   ORA-20112:
  --   ORA-20113: 'no active monitoring job found'
  --   ORA-20113: 'no active monitoring job found'
  -- 0 |<PS> =>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM 0 0 2 99.3% 0% 0.7% ""
  -- |<PR> DBS1.REGRESS.RDBMS.DEV.US.ORACLE.COM=> 100% 0% 0% "" |<PR> ...
  -- =>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM 92 7 99.3% 0% 0.7% "" |<PR> ...
  -- |<C> CAPTURE_USER1=>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM 2 0 0 0.E+00
  -- |<C> CAPTURE_USER1=>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM
  -- ORA-20111:
  -- ORA-20112:
  --   ORA-20100:
  --   ORA-20113: 'no active monitoring job found'
  --   ORA-20113: 'no active monitoring job found'
[oracle@DB-Server admin]$ 

如上所示,這個是一個模糊匹配,其實我是想要檢視ORA這類錯誤,那麼我要過濾掉哪一些沒有用的,搜尋的內容修改一下即可(當然也可以使用特殊引數,後面有講述),如下所示。

[oracle@DB-Server admin]$ grep "ORA-" utlspadv.sql
  --   ORA-XXXXX:        Monitoring already started. If for example you want 
  --   ORA-20111:
  --   ORA-20112:
  --   ORA-20113: 'no active monitoring job found'
  --   ORA-20113: 'no active monitoring job found'
  -- ORA-20111:
  -- ORA-20112:
  --   ORA-20100:
  --   ORA-20113: 'no active monitoring job found'
  --   ORA-20113: 'no active monitoring job found'
[oracle@DB-Server admin]$ 

2: 如果你想搜尋多個檔案是否包含某個字串,可以使用下面方式

grep "search content" filename1 filename2.... filenamen

grep "search content" *.sql

例如我想檢視當前目錄下,哪些sql指令碼包含檢視v$temp_space_header(注意:搜尋的內容如果包含特殊字元時,必須進行跳脫處理,如下所示)

[oracle@DB-Server admin]$ grep "v$temp_space_header" *.sql
catspacd.sql:drop public synonym v$temp_space_header;
catspacd.sql:drop public synonym gv$temp_space_header;
catspace.sql:create or replace view v_$temp_space_header as select * from v$temp_space_header;
catspace.sql:create or replace public synonym v$temp_space_header for v_$temp_space_header;
catspace.sql:create or replace view gv_$temp_space_header as select * from gv$temp_space_header;
catspace.sql:create or replace public synonym gv$temp_space_header
catspace.sql:            FROM gv$temp_space_header
[oracle@DB-Server admin]$ 

3:如果需要顯示搜尋文字在檔案中的行數,可以使用引數-n

[oracle@DB-Server admin]$ grep  -n "v$temp_space_header" *.sql
catspacd.sql:68:drop public synonym v$temp_space_header;
catspacd.sql:71:drop public synonym gv$temp_space_header;
catspace.sql:1952:create or replace view v_$temp_space_header as select * from v$temp_space_header;
catspace.sql:1953:create or replace public synonym v$temp_space_header for v_$temp_space_header;
catspace.sql:1956:create or replace view gv_$temp_space_header as select * from gv$temp_space_header;
catspace.sql:1957:create or replace public synonym gv$temp_space_header
catspace.sql:2357:            FROM gv$temp_space_header
[oracle@DB-Server admin]$ 

4: 如果搜尋時需要忽略大小寫問題,可以使用引數-i

 
[oracle@DB-Server admin]$ grep  "V$TEMP_SPACE_HEADER" *.sql
[oracle@DB-Server admin]$ grep -i "V$TEMP_SPACE_HEADER"  *.sql
catspacd.sql:drop public synonym v$temp_space_header;
catspacd.sql:drop public synonym gv$temp_space_header;
catspace.sql:create or replace view v_$temp_space_header as select * from v$temp_space_header;
catspace.sql:create or replace public synonym v$temp_space_header for v_$temp_space_header;
catspace.sql:create or replace view gv_$temp_space_header as select * from gv$temp_space_header;
catspace.sql:create or replace public synonym gv$temp_space_header
catspace.sql:            FROM gv$temp_space_header
[oracle@DB-Server admin]$ 

另外,例如檢查安裝的MySQL元件

[root@DB-Server init.d]# rpm -qa | grep -i mysql
 
MySQL-devel-5.6.23-1.linux_glibc2.5
 
MySQL-client-5.6.23-1.linux_glibc2.5
 
MySQL-server-5.6.23-1.linux_glibc2.5

5:從檔案內容查詢不匹配指定字串的行:

$ grep –v "被查詢的字串" 檔名

例如查詢某些進程時,我們不想顯示包含命令grep ora_mmon的進程,如下所示

[oracle@DB-Server admin]$ ps -ef  | grep ora_mmon  
oracle   16675 16220  0 00:09 pts/1    00:00:00 grep ora_mmon
oracle   21412     1  0 Aug22 ?        00:00:07 ora_mmon_gsp
[oracle@DB-Server admin]$ ps -ef  | grep ora_mmon  | grep -v grep
oracle   21412     1  0 Aug22 ?        00:00:07 ora_mmon_gsp
[oracle@DB-Server admin]$ 

6:搜尋、查詢匹配的行數:

$ grep -c "被查詢的字串" 檔名

[oracle@DB-Server admin]$ 
[oracle@DB-Server admin]$ grep   "v$temp_space_header" *.sql
catspacd.sql:drop public synonym v$temp_space_header;
catspacd.sql:drop public synonym gv$temp_space_header;
catspace.sql:create or replace view v_$temp_space_header as select * from v$temp_space_header;
catspace.sql:create or replace public synonym v$temp_space_header for v_$temp_space_header;
catspace.sql:create or replace view gv_$temp_space_header as select * from gv$temp_space_header;
catspace.sql:create or replace public synonym gv$temp_space_header
catspace.sql:            FROM gv$temp_space_header
[oracle@DB-Server admin]$ grep -c  "v$temp_space_header"  catspacd.sql
2
[oracle@DB-Server admin]$ grep -c  "v$temp_space_header"  catspace.sql
5
[oracle@DB-Server admin]$ 

7:有些場景,我們並不知道檔案型別、或那些檔案包含有我們需要搜尋的字串,那麼可以遞回搜尋某個目錄以及子目錄下的所有檔案

[oracle@DB-Server ~]$ grep -r "v$temp_space_header" /u01/app/oracle/product/11.1.0/dbhome_1/rdbms/admin/

/u01/app/oracle/product/11.1.0/dbhome_1/rdbms/admin/catspace.sql:create or replace view v_$temp_space_header as select * from v$temp_space_header;

/u01/app/oracle/product/11.1.0/dbhome_1/rdbms/admin/catspace.sql:create or replace public synonym v$temp_space_header for v_$temp_space_header;

/u01/app/oracle/product/11.1.0/dbhome_1/rdbms/admin/catspace.sql:create or replace view gv_$temp_space_header as select * from gv$temp_space_header;

/u01/app/oracle/product/11.1.0/dbhome_1/rdbms/admin/catspace.sql:create or replace public synonym gv$temp_space_header

/u01/app/oracle/product/11.1.0/dbhome_1/rdbms/admin/catspace.sql: FROM gv$temp_space_header

/u01/app/oracle/product/11.1.0/dbhome_1/rdbms/admin/catspacd.sql:drop public synonym v$temp_space_header;

/u01/app/oracle/product/11.1.0/dbhome_1/rdbms/admin/catspacd.sql:drop public synonym gv$temp_space_header;

[oracle@DB-Server ~]$

8:如果我們只想獲取那些檔案包含搜尋的內容,那麼可以使用下命令

[oracle@DB-Server ~]$ grep -H -r "v$temp_space_header" /u01/app/oracle/product/11.1.0/dbhome_1/rdbms/admin/ | cut -d: -f1

/u01/app/oracle/product/11.1.0/dbhome_1/rdbms/admin/catspace.sql

/u01/app/oracle/product/11.1.0/dbhome_1/rdbms/admin/catspace.sql

/u01/app/oracle/product/11.1.0/dbhome_1/rdbms/admin/catspace.sql

/u01/app/oracle/product/11.1.0/dbhome_1/rdbms/admin/catspace.sql

/u01/app/oracle/product/11.1.0/dbhome_1/rdbms/admin/catspace.sql

/u01/app/oracle/product/11.1.0/dbhome_1/rdbms/admin/catspacd.sql

/u01/app/oracle/product/11.1.0/dbhome_1/rdbms/admin/catspacd.sql

[oracle@DB-Server ~]$ grep -H -r "v$temp_space_header" /u01/app/oracle/product/11.1.0/dbhome_1/rdbms/admin/ | cut -d: -f1 | uniq

/u01/app/oracle/product/11.1.0/dbhome_1/rdbms/admin/catspace.sql

/u01/app/oracle/product/11.1.0/dbhome_1/rdbms/admin/catspacd.sql

[oracle@DB-Server ~]$

9:如果只想獲取和整個搜尋字元匹配的內容,那麼可以使用引數w

你可以對比一下兩者的區別

[oracle@DB-Server admin]$ grep -w "ORA" utlspadv.sql
  --   ORA-XXXXX:        Monitoring already started. If for example you want 
  --   ORA-20111:
  --   ORA-20112:
  --   ORA-20113: 'no active monitoring job found'
  --   ORA-20113: 'no active monitoring job found'
  -- ORA-20111:
  -- ORA-20112:
  --   ORA-20100:
  --   ORA-20113: 'no active monitoring job found'
  --   ORA-20113: 'no active monitoring job found'
[oracle@DB-Server admin]$ grep  "ORA" utlspadv.sql
  --   ORA-XXXXX:        Monitoring already started. If for example you want 
  --   ORA-20111:
  --   ORA-20112:
  --   ORA-20113: 'no active monitoring job found'
  --   ORA-20113: 'no active monitoring job found'
  -- 0 |<PS> =>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM 0 0 2 99.3% 0% 0.7% ""
  -- |<PR> DBS1.REGRESS.RDBMS.DEV.US.ORACLE.COM=> 100% 0% 0% "" |<PR> ...
  -- =>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM 92 7 99.3% 0% 0.7% "" |<PR> ...
  -- |<C> CAPTURE_USER1=>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM 2 0 0 0.E+00
  -- |<C> CAPTURE_USER1=>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM
  -- ORA-20111:
  -- ORA-20112:
  --   ORA-20100:
  --   ORA-20113: 'no active monitoring job found'
  --   ORA-20113: 'no active monitoring job found'
[oracle@DB-Server admin]$ 

10: grep命令結合find命令搜尋

[oracle@DB-Server admin]$ find . -name '*.sql' -exec grep -i 'v$temp_space_header' {} ; -print
create or replace view v_$temp_space_header as select * from v$temp_space_header;
create or replace public synonym v$temp_space_header for v_$temp_space_header;
create or replace view gv_$temp_space_header as select * from gv$temp_space_header;
create or replace public synonym gv$temp_space_header
            FROM gv$temp_space_header
./catspace.sql
drop public synonym v$temp_space_header;
drop public synonym gv$temp_space_header;
./catspacd.sql
[oracle@DB-Server admin]$ 

11: egrep -w -R 'word1|word2' ~/klbtmp

12: vi命令其實也能搜尋檔案裡面的內容,只不過沒有grep命令功能那麼方便、強大。

grep使用簡明及正規表示式  http://www.linuxidc.com/Linux/2013-08/88534.htm

 

Linux下Shell程式設計——grep命令的基本運用 http://www.linuxidc.com/Linux/2013-06/85525.htm

 

grep 命令詳解及相關事例 http://www.linuxidc.com/Linux/2014-07/104041.htm

 

Linux基礎命令之grep詳解 http://www.linuxidc.com/Linux/2013-07/87919.htm

 

設定grep高亮顯示匹配項 http://www.linuxidc.com/Linux/2014-09/106871.htm

 

Linux grep命令學習與總結 http://www.linuxidc.com/Linux/2014-10/108112.htm

 

14 個 grep 命令的例子 http://www.linuxidc.com/Linux/2015-05/117626.htm

 

本文永久更新連結地址http://www.linuxidc.com/Linux/2016-08/134570.htm


IT145.com E-mail:sddin#qq.com