在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命令功能那麼方便、強大。
參考資料:
http://www.cyberciti.biz/faq/howto-search-find-file-for-text-string/
http://xxxxxx/Linuxjc/1152257.html TechArticle