Linux如何搜索查找文件里面内容

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]$

clip_image001

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]$

clip_image002

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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/15034.html