编程知识 cdmana.com

Find content in Linux file

stay Linux In the system , How to search 、 Search the contents of the file ? This should be system maintenance 、 The most common requirements encountered in Management . So here is , Summarize how to search 、 Look up the contents of the file .

Search for 、 Look up the contents of the file , The most commonly used is grep command , And then there is egrep, vi Commands can also search the contents of a file

1: Search whether a file contains a string , Use grep "search content" filename1, for example

$ grep ORA alert_gsp.log

$ grep "ORA" alert_gsp.log

For example, we need to search 、 lookup utlspadv.sql The file contains ORA Character content of

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

As shown above , This is a fuzzy match , Actually, I want to see ORA Such mistakes , So I'm going to filter out the useless ones , The content of the search can be modified ( Of course, special parameters can also be used , I'll tell you later ), As shown below .

[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: If you want to search multiple files for a string , You can use the following

grep "search content" filename1 filename2.... filenamen

grep "search content" *.sql

For example, I want to check the current directory , Which? sql The script contains views v$temp_space_header( Be careful : If the search content contains special characters , It has to be escaped , As shown below )

[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: If you need to display the number of lines of search text in the file , You can use parameters -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: If you need to ignore case when searching , You can use parameters -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]$ 

in addition , For example, check the installed MySQL Components

[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: Finds a line from the file content that does not match the specified string :

$ grep –v " The string being searched " file name

For example, when looking for certain processes , We don't want to show include commands grep ora_mmon The process of , As shown below

[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: Search for 、 Find the number of matching rows :

$ grep -c " The string being searched " file name

[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: Some scenes , We don't know the file type 、 Or those files contain strings that we need to search for , Then you can recursively search all the files in a directory and subdirectories

[oracle@DB-Server ~] g r e p r " v 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_ t e m p s p a c e h e a d e r a s s e l e c t f r o m 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_ t e m p s p a c e h e a d e r a s s e l e c t f r o m g v 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: If we just want to get those files that contain the content of the search , Then you can use the command

[oracle@DB-Server ~] g r e p H r " v 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 ~] g r e p H r " v 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: If you only want to get the content that matches the whole search character , So you can use parameters w

You can compare the two

[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 Combination of orders find The command to search

[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 The command can also search the contents of the file , Just not grep The command function is so convenient 、 Powerful .

Reference material :

http://www.cyberciti.biz/faq/howto-search-find-file-for-text-string/

That's all Liangxu tutorial website For all of you Linux Related knowledge .

版权声明
本文为[Pioneer of code farmers]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/02/20210201031608308p.html

Scroll to Top