linuxmint
[root@localhost test]#
说明:
输出test.txt文件中含有从test2.txt文件中读取出的关键词的内容行
实例3:从文件中读取关键词进行搜索 且显示行号
命令:
cat test.txt | grep -nf test2.txt
输出:
[root@localhost test]# cat test.txt
hnlinux
peida.cnblogs.com
ubuntu
ubuntu linux
redhat
Redhat
linuxmint
[root@localhost test]# cat test2.txt
linux
Redhat
[root@localhost test]# cat test.txt | grep -nf test2.txt
1:hnlinux
4:ubuntu linux
6:Redhat
7:linuxmint
[root@localhost test]#
说明:
输出test.txt文件中含有从test2.txt文件中读取出的关键词的内容行,并显示每一行的行号
实例5:从文件中查找关键词
命令:
grep 'linux' test.txt
输出:
[root@localhost test]# grep 'linux' test.txt
hnlinux
ubuntu linux
linuxmint
[root@localhost test]# grep -n 'linux' test.txt
1:hnlinux
4:ubuntu linux
7:linuxmint
[root@localhost test]#
说明:
实例6:从多个文件中查找关键词
命令:
grep 'linux' test.txt test2.txt
输出:
[root@localhost test]# grep -n 'linux' test.txt test2.txt
test.txt:1:hnlinux
test.txt:4:ubuntu linux
test.txt:7:linuxmint
test2.txt:1:linux
[root@localhost test]# grep 'linux' test.txt test2.txt
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字符的内容行
- redis的热点数据缓存 redis热点数据切换
- 如何修改戴尔服务器的IP地址? 戴尔服务器ip地址怎么改
- mysql中ext
- 优惠券功能的业务流程设计图谱 优惠券redis处理
- redis通配符的使用
- redis是开发工具吗 redis的开发人是谁
- mysql备份一个表的数据 备份一个mysql库
- mysql中删除记录的命令 mysql删除中继日志
- mysql 判断 mysql的判断语句
- 屏蔽数据库的复杂性 mysql数据库屏蔽权限
