1)grep,egrep
grep
-i ##忽略字母大小写
-v ##条件取反
-c ##统计匹配行数
-q ##静默,无任何输出
-n ##显示匹配结果所在的行号
-q
[root@localhost mnt]# grep '172.25.254.225' /etc/hosts && echo 'YES' || echo 'NO'
NO
[root@localhost mnt]# vim /etc/hosts
[root@localhost mnt]# grep '172.25.254.254' /etc/hosts && echo 'YES' || echo 'NO'
172.25.254.254 classroom.example.com
172.25.254.254 content.example.com
YES
[root@localhost mnt]# grep -q '172.25.254.254' /etc/hosts && echo 'YES' || echo 'NO'
YES
-c
[root@localhost mnt]# egrep -c '/root' /etc/passwd
2
-v
[root@localhost mnt]# egrep -vc '/root' /etc/passwd
37
-n
[root@localhost mnt]# egrep -n '/root' /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost mnt]# egrep -m10 '/sbin/nologin' /etc/passwd 查看含有/sbin/nologin的前10行
bin:x:1:1:bin:/bin:/sbin/nologin
2)基本元字符:^ 和$ , 其中 ^表示以什么字符开头的,表示已什么结尾的
[root@localhost mnt]# egrep '^root' /etc/passwd 查看文件 /etc/passwd 中以root开头的行
root:x:0:0:root:/root:/bin/bash
[root@localhost mnt]# vim haha
[root@localhost mnt]# cat haha
hello nihao
tianqi nihao
hahahaha
westos hello
[root@localhost mnt]# egrep 'nihao$' haha
hello nihao
tianqi nihao
过滤非空行,和空行
[root@localhost mnt]# egrep '.' haha 过滤非空行
hello nihao
tianqi nihao
hahahaha
westos hello
[root@localhost mnt]# egrep -v '.' haha 过滤空行
[root@localhost mnt]# egrep '^$' haha 过滤空行
基本元字符: + ? *
[root@localhost mnt]# cat hh
hello hello hello
hello hellooooooo
hellooo
hellooooo
world
[root@localhost mnt]# egrep 'o' hh
hello hello hello
hello hellooooooo
hellooo
hellooooo
world
[root@localhost mnt]# egrep 'o+' hh
hello hello hello
hello hellooooooo
hellooo
hellooooo
world
[root@localhost mnt]# egrep 'hello(o)' hh
hello hellooooooo
hellooo
hellooooo
[root@localhost mnt]# egrep '(oo){2}' hh
hello hellooooooo
hellooooo
[root@localhost mnt]# egrep '(oo){3}' hh
hello hellooooooo
[root@localhost mnt]# egrep '(oo){3,}' hh
hello hellooooooo
[root@localhost mnt]# egrep '(oo){2,}' hh
hello hellooooooo
hellooooo
[root@localhost mnt]# egrep '(oo){1,3}' hh
hello hellooooooo
hellooo
hellooooo
[root@localhost mnt]#
3)cut命令
cut -d ##指定分隔符
cut -d : -f 1-3 /etc/passwd ##指定分隔符为:,显示第1到3列
cut -c 1,4 /etc/passwd ##显示第一和第四个字符
练习:获取主机IP
[root@localhost mnt]# ifconfig eth0 | grep "inet " | cut -d " " -f 10
172.25.254.225
ifconfig eth0 查看eht0网卡的信息
grep "inet " 以"inet "为分割点经过分割
cut -d " " -f 10 以" "为分隔符的第10个字段的内容
[root@localhost mnt]# ifconfig eth0 | grep "inet " | awk '{print $2}'
172.25.254.225
检测网卡是否连接
[root@localhost mnt]# vim ping.sh
[root@localhost mnt]# sh ping.sh 250
172.25.254.250 is up
[root@localhost mnt]# sh ping.sh 254
172.25.254.254 is down
[root@localhost mnt]# cat ping.sh
#!/bin/bash
ping -c1 -w1 172.25.254.$1 &> /dev/null && echo 172.25.254.$1 is up || echo 172.25.254.$1 is down
[root@localhost mnt]#
sort命令:排序
sort
-n ##纯数字排序
-r ##倒序
-u ##去掉重复数字
-o ##输出到指定文件中
-t ##指定分隔符
-k ##指定要排序的列
[root@server ~]# sort westos
1
12
123
2
3
32
5
51
6
7
[root@server ~]# sort -n westos
1
2
3
5
6
7
12
32
51
123
[root@server ~]# sort -u westos
1
12
123
2
3
32
5
51
6
7
[root@server ~]# sort -t : -k 2 westos
2:0
12:10
2:12
3:2
51:20
5:21
123:22
32:31
5:4
6:4
1:5
51:55
123:66
7:79
[root@server ~]# sort -nt : -k 2 westos
2:0
3:2
5:4
6:4
1:5
12:10
2:12
51:20
5:21
123:22
32:31
51:55
123:66
7:79
[root@server ~]# sort -nt : -k 2 westos -o /mnt/file
5)uniq命令:对重复字符处理
uniq
-u ##显示唯一的行
-d ##显示重复的行
-c ##每行显示一次并统计重复次数
[root@server ~]# sort -n westos | uniq -c
1 0
1 1
2 2
1 4
1 6
1 9
2 10
1 20
1 22
2 31
1 55
[root@server ~]# sort -n westos | uniq -d
2
10
31
[root@server ~]# sort -n westos | uniq -u
0
1
4
6
9
20
22
55
练习:
[root@localhost mnt]# ls -l /tmp/
total 0
drwx------. 2 root root 23 12月 27 00:26 ssh-mXyt10p9qRFs
drwx------. 3 root root 16 12月 27 00:25 systemd-private-G3f3D4
drwx------. 3 root root 16 12月 27 00:26 systemd-private-VuoudY
drwx------. 3 root root 16 12月 27 00:25 systemd-private-wFGa2M
[root@localhost mnt]# ls -Sl /tmp/ | head -2 | cut -d " " -f 9
ssh-mXyt10p9qRFs
ls -Sl 是按照大小排序列出/tmp下的文件
head -2 显示前两行
cut -d " " -f 9 以空格为分隔符,剪切第9个字符串