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

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

clip_image003

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

你可以对比一下两者的区别

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

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