Linux查找文件內容的常用命令方法。
創(chuàng)新互聯堅持“要么做到,要么別承諾”的工作理念,服務領域包括:做網站、網站設計、企業(yè)官網、英文網站、手機端網站、網站推廣等服務,滿足客戶于互聯網時代的木蘭網站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯網解決方案。努力成為您成熟可靠的網絡建設合作伙伴!
從文件內容查找匹配指定字符串的行:
$ grep "被查找的字符串" 文件名
例子:在當前目錄里第一級文件夾中尋找包含指定字符串的.in文件
grep "thermcontact" */*.in
從文件內容查找與正則表達式匹配的行:
$ grep –e “正則表達式” 文件名
查找時不區(qū)分大小寫:
$ grep –i "被查找的字符串" 文件名
查找匹配的行數:
$ grep -c "被查找的字符串" 文件名
從文件內容查找不匹配指定字符串的行:
$ grep –v "被查找的字符串" 文件名
學習更多l(xiāng)inux知識《Linux就該這么學》,從根目錄開始查找所有擴展名為.log的文本文件,并找出包含”ERROR”的行
find / -type f -name "*.log" | xargs grep "ERROR"
例子:從當前目錄開始查找所有擴展名為.in的文本文件,并找出包含”thermcontact”的行
find . -name "*.in" | xargs grep "thermcontact"
搜索、查找文件當中的內容,一般最常用的是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
你可以對比一下兩者的區(qū)別
[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命令功能那么方便、強大。
Linux下查看文件內容的命令
查看文件內容的命令:
cat 由第一行開始顯示內容,并將所有內容輸出
tac 從最后一行倒序顯示內容,并將所有內容輸出
more根據窗口大小,一頁一頁的現實文件內容
less 和more類似,但其優(yōu)點可以往前翻頁,而且進行可以搜索字符
head 只顯示頭幾行
tail 只顯示最后幾行
nl 類似于cat -n,顯示時輸出行號 《Linux就該這么學》 一起學習linux
tailf 類似于tail -f
linux系統(tǒng)查看文件內容的命令包括但不僅限于cat、tac、more、less、head、tail、nl、tailf,tail –f,
本文題目:linux查找正文命令 linux查找命令位置
文章地址:http://muchs.cn/article16/dojhpgg.html
成都網站建設公司_創(chuàng)新互聯,為您提供品牌網站制作、外貿建站、網站改版、Google、小程序開發(fā)、品牌網站設計
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯