In Linux, how does one search for the content in a file? in linux, how does one search for the content?
In Linux, how does one search and search for the content in a file? This should be the most common requirement in system maintenance and management. The following describes how to search and find the content in a file.
The grep command is commonly used to search and find the content in a file. In addition, the egrep and vi commands can also search for the content in the 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, you need to search for and find the characters in the utlspadv. SQL file that contain 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]$
As shown above, this is a fuzzy match. In fact, I want to view errors such as ORA, so I want to filter out which ones are useless, modify the search content (you can also use special parameters, as described 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]$
2: If you want to search whether multiple files contain a certain string, you can use the following method:
Grep "search content" filename1 filename2.... filenamen
Grep "search content" *. SQL
For example, I want to check which SQL scripts in the current directory contain the view v $ temp_space_header (Note: if the search content contains special characters, it must 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]$
3: To display the number of lines in the search text file, you can use the-n parameter.
[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 sensitivity during search, you can use the-I parameter.
[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 component
[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: Search for rows that do not match the specified string from the file content:
$ Grep-v "searched string" File Name
For example, when searching for some processes, we do not want to display the processes that contain the grep ora_mmon Command, 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 and find the matched rows:
$ Grep-c "searched string" 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]$
7: In some scenarios, we do not know the file type, or the file packages contain the strings we need to search for, so we can Recursively search all the files in a directory and subdirectory.
[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: If you only want to retrieve the files that contain the searched content, run the following command:
[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: If you only want to obtain the content that matches the entire search character, you can use the parameter w
You can compare the differences between 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 command combined with find command 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: The vi command can actually search the content in the file, but it is not as convenient and powerful as the grep command.
References:
Http://www.cyberciti.biz/faq/howto-search-find-file-for-text-string/