linux查找正文命令 linux查找命令位置

linux 查找文件内容命令

Linux查找文件内容的常用命令方法。

创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:做网站、网站设计、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的木兰网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

从文件内容查找匹配指定字符串的行:

$ grep "被查找的字符串" 文件名

例子:在当前目录里第一级文件夹中寻找包含指定字符串的.in文件

grep "thermcontact" */*.in

从文件内容查找与正则表达式匹配的行:

$ grep –e “正则表达式” 文件名

查找时不区分大小写:

$ grep –i "被查找的字符串" 文件名

查找匹配的行数:

$ grep -c "被查找的字符串" 文件名

从文件内容查找不匹配指定字符串的行:

$ grep –v "被查找的字符串" 文件名

学习更多linux知识《Linux就该这么学》,从根目录开始查找所有扩展名为.log的文本文件,并找出包含”ERROR”的行

find / -type f -name "*.log" | xargs grep "ERROR"

例子:从当前目录开始查找所有扩展名为.in的文本文件,并找出包含”thermcontact”的行

find . -name "*.in" | xargs grep "thermcontact"

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

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

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

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

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

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

[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命令结合find命令搜索

[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命令其实也能搜索文件里面的内容,只不过没有grep命令功能那么方便、强大。

linux 查看文件内容的命令

Linux下查看文件内容的命令

查看文件内容的命令:

cat 由第一行开始显示内容,并将所有内容输出

tac 从最后一行倒序显示内容,并将所有内容输出

more根据窗口大小,一页一页的现实文件内容

less 和more类似,但其优点可以往前翻页,而且进行可以搜索字符

head 只显示头几行

tail 只显示最后几行

nl 类似于cat -n,显示时输出行号 《Linux就该这么学》 一起学习linux

tailf 类似于tail -f

linux查看文本内容的命令

linux系统查看文件内容的命令包括但不仅限于cat、tac、more、less、head、tail、nl、tailf,tail –f,


网页题目:linux查找正文命令 linux查找命令位置
标题路径:http://hbruida.cn/article/dojhpgg.html