Linux如何搜索查⽂件⾥⾯内容
1:搜索某个⽂件⾥⾯是否包含字符串,使⽤grep "search content" filename1
eg:搜索、查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:
linux怎么读取文件--  ORA-20113: 'no active monitoring job found'
--  ORA-20113: 'no active monitoring job found'
-- 0 |<PS> =>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM 00299.3% 0% 0.7% ""
-- |<PR> DBS1.REGRESS.RDBMS.DEV.US.ORACLE.COM=> 100% 0% 0% "" |<PR> ...
-
- =>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM 92799.3% 0% 0.7% "" |<PR> ...
-- |<C> CAPTURE_USER1=>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM 2000.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" 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_h
eader;
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]$
5:从⽂件内容查不匹配指定字符串的⾏:
$ grep –v "被查的字符串" ⽂件名
例如查某些进程时,我们不想显⽰包含命令grep ora_mmon的进程,如下所⽰
[oracle@DB-Server admin]$ ps -ef  | grep ora_mmon
oracle  1667516220000:09 pts/100:00:00 grep ora_mmon
oracle  2141210 Aug22 ?        00:00:07 ora_mmon_gsp
[oracle@DB-Server admin]$ ps -ef  | grep ora_mmon  | grep -v grep
oracle  2141210 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:有些场景,我们并不知道⽂件类型、或那些⽂件包含有我们需要搜索的字符串,那么可以递归搜索某个⽬录以及⼦⽬录下的所有⽂件grep -r 关键字查的⽬录
oracle@DB-Server ~]$ grep -r "v\$temp_space_header" ./
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 00299.3% 0% 0.7% ""
-- |<PR> DBS1.REGRESS.RDBMS.DEV.US.ORACLE.COM=> 100% 0% 0% "" |<PR> ...
-
- =>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM 92799.3% 0% 0.7% "" |<PR> ...
-- |<C> CAPTURE_USER1=>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM 2000.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命令搜索
find ./ -name ‘.’ | xargs grep “hello”
[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]$

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。