linux命令greap linux命令grep 查询文件( 五 )


test.txt:hnlinux
test.txt:ubuntu linux
test.txt:linuxmint
test2.txt:linux
[root@localhost test]#
说明:
多文件时,输出查询到的信息内容行时,会把文件的命名在行最前面输出并且加上":"作为标示符
实例7:grep不显示本身进程
命令:
ps aux|grep \[s]sh
ps aux | grep ssh | grep -v "grep"
输出:
[root@localhost test]# ps aux|grep ssh
root27200.00.0626561212 ?SsNov020:00 /usr/sbin/sshd
root168340.00.0880883288 ?Ss19:530:00 sshd: root@pts/0
root169010.00.061180764 pts/0S+20:310:00 grep ssh
[root@localhost test]# ps aux|grep \[s]sh]
[root@localhost test]# ps aux|grep \[s]sh
root27200.00.0626561212 ?SsNov020:00 /usr/sbin/sshd
root168340.00.0880883288 ?Ss19:530:00 sshd: root@pts/0
[root@localhost test]# ps aux | grep ssh | grep -v "grep"
root27200.00.0626561212 ?SsNov020:00 /usr/sbin/sshd
root168340.00.0880883288 ?Ss19:530:00 sshd: root@pts/0
说明:
实例8:找出已u开头的行内容
命令:
cat test.txt |grep ^u
输出:
[root@localhost test]# cat test.txt |grep ^u
ubuntu
ubuntu linux
[root@localhost test]#
说明:
实例9:输出非u开头的行内容
命令:
cat test.txt |grep ^[^u]
输出:
[root@localhost test]# cat test.txt |grep ^[^u]
hnlinux
peida.cnblogs.com
redhat
Redhat
linuxmint
[root@localhost test]#
说明:
实例10:输出以hat结尾的行内容
命令:
cat test.txt |grep hat$
输出:
[root@localhost test]# cat test.txt |grep hat$
redhat
Redhat
[root@localhost test]#
说明:
实例11:输出ip地址
命令:
ifconfig eth0|grep -E "([0-9]{1,3}\.){3}[0-9]"
输出:
[root@localhost test]# ifconfig eth0|grep "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"
inet addr:192.168.120.204Bcast:192.168.120.255Mask:255.255.255.0
[root@localhost test]# ifconfig eth0|grep -E "([0-9]{1,3}\.){3}[0-9]"
inet addr:192.168.120.204Bcast:192.168.120.255Mask:255.255.255.0
[root@localhost test]#
说明:
实例12:显示包含ed或者at字符的内容行
命令:
cat test.txt |grep -E "ed|at"
输出:
[root@localhost test]# cat test.txt |grep -E "peida|com"
peida.cnblogs.com
[root@localhost test]# cat test.txt |grep -E "ed|at"
redhat
Redhat
[root@localhost test]#
说明:
实例13:显示当前目录下面以.txt 结尾的文件中的所有包含每个字符串至少有7个连续小写字符的字符串的行
命令:
grep '[a-z]\{7\}' *.txt
输出:
[root@localhost test]# grep '[a-z]\{7\}' *.txt
test.txt:hnlinux
test.txt:peida.cnblogs.com
test.txt:linuxmint
[root@localhost test]#
实例14:日志文件过大,不好查看 , 我们要从中查看自己想要的内容,或者得到同一类数据,比如说没有404日志信息的
命令:
grep '.' access1.log|grep -Ev '404'access2.log
grep '.' access1.log|grep -Ev '(404|/photo/|/css/)'access2.log
grep '.' access1.log|grep -E '404'access2.log
输出:
[root@localhost test]# grep “.”access1.log|grep -Ev “404”access2.log
说明:上面3句命令前面两句是在当前目录下对access1.log文件进行查找 , 找到那些不包含404的行,把它们放到access2.log中,后面去掉’v’,即是把有404的行放入access2.log
Linux命令之grepgrep(global search regular expression(RE)and print out the line 即linux命令greap:全面搜索正则表达式并把行打印出来)命令是一个强大的文本搜索工具linux命令greap,它能使用正则表达式搜索文本,并把匹配的行打印出来 。

推荐阅读