Linux查找文件内容
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
Linux查找文件內(nèi)容
使用vim命令查找文件內(nèi)容
我們可以使用/string命令來向前(Forward)查找字符串string,按下回車后,光標(biāo)就能跳到正確的地方。在這個命令中,/后的字符是我們想要查找的字符,而回車鍵則表明了命令的結(jié)束。
有時想要查找的內(nèi)容并不僅在一處,我們可以在整個文章中進(jìn)行查找:/可以繼續(xù)剛才的查找操作。我們還可以使用n命令來繼續(xù)剛才的查找命令。這兩個命令都能達(dá)到同樣的效果。
一般來說,在進(jìn)行查找時總是在向前查找。我們也可以使用?命令向后(Backward)查找。N也是逆向查找命令,他可以實(shí)現(xiàn)立即反向查找。
如果我們想要停止這一查找,可以使用ctrl+C命令。
?
使用grep命令查找文件內(nèi)容
Linux系統(tǒng)中g(shù)rep命令是一種強(qiáng)大的文本搜索工具,它能使用正則表達(dá)式搜索文本,并把匹配的行打印出來。
常用的命令參數(shù)有:
-c:只輸出匹配行的計數(shù)。
-i:不區(qū)分大小寫
-n:顯示匹配行及行號。
-h:查詢多文件時不顯示文件名。
-v:顯示不包含匹配文本的所有行。
使用示例
查找以log結(jié)尾的文件中的"error"文本(忽略error大小寫)匹配的行的計數(shù)。
? logs grep -ic 'error' *.log error_uc4s.log:0 error_uc4sadmin.log:3758 error_ucvms.log:301 uc4s.log:0 uc4s_log.2016-10-12.log:207 ...... ...... ......查找文件中"ERROR"文本并顯示所在行
? logs grep -n 'ERROR' uc4sadmin.log 1:2016-08-12 15:20:28.850 ERROR [RMI TCP Connection(2)-127.0.0.1] com.zuche.confcenter.client.manager.DefaultConfigCenterManager:108 - init confcenter fail,the reason is not found conf_center.properties file,the current project as common project and read common datasource,domain is null 2:2016-08-12 15:20:31.860 ERROR [RMI TCP Connection(2)-127.0.0.1] com.uc.ucBase.common.func.FuncConfirm:168 - funcClassName is not find 82:2016-08-12 15:20:37.510 ERROR [RMI TCP Connection(2)-127.0.0.1] com.uc.ucBase.util.InitDomainUtil:39 - remote domain = {loginImg=testImg, ucASMSURL=http://asmstest.maimaiche.com/ucasms, ucDomain=maimaiche.com, ucCRMURL=http://crmtest.maimaiche.com/uccrm, ucAdminURL=http://admintest.maimaiche.com/ucadmin, ucVMURL=http://vmstest.maimaiche.com/ucvms, ucbi=http://bitest.maimaiche.com/ucbi, ucwap=http://waptest.zhunxinche.com/ucwap, ucweb=http://webtest.zhunxinche.com/ucweb, ucRMURL=http://statictest.zhunxinche.com, env=test, zucheMapiURL=http://mapitest.zuche.com:9080, ucURMSURL=${ucURMSURL}, ucCMSURL=http://cmstest.maimaiche.com/uccms, ucRMimageURL=http://imagetest.zhunxinche.com, loginUrl=http://admintest.maimaiche.com/ucadmin/login.jsp, ucFINURL=http://fintest.maimaiche.com/ucfin, baseContextPath=http://admintest.maimaiche.com/ucadmin, ucRMjsURL=http://jstest.zhunxinche.com, ucRMcssURL=http://csstest.zhunxinche.com}這里查出來有三條結(jié)果,“ERROR”分別在1,2還有82行。
那如果想看 82 行的上文和下文怎么辦呢 ,sed 可以實(shí)現(xiàn)。這里打印82行到90行,p是sed的打印指令。
? logs sed -n '82,90p' uc4sadmin.log 2016-08-12 15:20:37.510 ERROR [RMI TCP Connection(2)-127.0.0.1] com.uc.ucBase.util.InitDomainUtil:39 - remote domain = {loginImg=testImg, ucASMSURL=http://asmstest.maimaiche.com/ucasms, ucDomain=maimaiche.com, ucCRMURL=http://crmtest.maimaiche.com/uccrm, ucAdminURL=http://admintest.maimaiche.com/ucadmin, ucVMURL=http://vmstest.maimaiche.com/ucvms, ucbi=http://bitest.maimaiche.com/ucbi, ucwap=http://waptest.zhunxinche.com/ucwap, ucweb=http://webtest.zhunxinche.com/ucweb, ucRMURL=http://statictest.zhunxinche.com, env=test, zucheMapiURL=http://mapitest.zuche.com:9080, ucURMSURL=${ucURMSURL}, ucCMSURL=http://cmstest.maimaiche.com/uccms, ucRMimageURL=http://imagetest.zhunxinche.com, loginUrl=http://admintest.maimaiche.com/ucadmin/login.jsp, ucFINURL=http://fintest.maimaiche.com/ucfin, baseContextPath=http://admintest.maimaiche.com/ucadmin, ucRMjsURL=http://jstest.zhunxinche.com, ucRMcssURL=http://csstest.zhunxinche.com} 2016-08-12 15:20:37.512 ERROR [RMI TCP Connection(2)-127.0.0.1] com.uc.ucBase.util.InitDomainUtil:42 - local domain = {mapKey=595c2643c16cefd2fdece3fb111042f7, baseContextPath=http://4sadmintest.maimaiche.com/uc4sadmin, env=development} 2016-08-12 15:23:17.624 ERROR [RMI TCP Connection(2)-127.0.0.1] com.zuche.confcenter.client.manager.DefaultConfigCenterManager:108 - init confcenter fail,the reason is not found conf_center.properties file,the current project as common project and read common datasource,domain is null 2016-08-12 15:23:18.893 ERROR [RMI TCP Connection(2)-127.0.0.1] com.uc.ucBase.common.func.FuncConfirm:168 - funcClassName is not find java.lang.NullPointerExceptionat java.util.Properties$LineReader.readLine(Properties.java:434)at java.util.Properties.load0(Properties.java:353)at java.util.Properties.load(Properties.java:341)at com.uc.ucBase.util.PropertiesUtil.doLoad(PropertiesUtil.java:101)========END========
轉(zhuǎn)載于:https://my.oschina.net/xinxingegeya/blog/846915
總結(jié)
以上是生活随笔為你收集整理的Linux查找文件内容的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux 基础学习(第一节)
- 下一篇: JavaScript中Ajax源码