1.6. 2017-11-10-练习-grep-文本

1.6.1. 练习1-grep使用

1、 显示/proc/meminfo文件中以大写或者小写S开头的行

[root@centos7 sh]# egrep "^[s|S]" /proc/meminfo
SwapCached:            0 kB
SwapTotal:      134217724 kB
SwapFree:       134217724 kB
Shmem:              9468 kB
Slab:              95508 kB
SReclaimable:      42972 kB
SUnreclaim:        52536 kB

2、显示/etc/passwd文件中默认shell为非/sbin/nologin的用户。

[root@centos7 sh]# egrep  -v "/sbin/nologin$" /etc/passwd | cut -d ":" -f1
root
sync
shutdown
halt
zhaojiedi
u2
u3
u1

3、显示/etc/passwd文件中默认shell为/bin/bash的用户,进一步进现实上述结果中id最大的用户

[root@centos7 sh]# cat /etc/passwd | egrep "/bin/bash$" | sort -t ":" -k 3 -n |tail -n 1 | cut -d ":" -f1
u1

4、找出/etc/passwd文件中的一位数或者2位数。

[root@centos7 sh]# cat /etc/passwd | egrep "\b[0-9]{1,2}\b" -o

5、显示/boot/grub/grub.conf中以至少一个空白字符开头的行

[root@centos7 grub2]# cat /boot/grub2/grub.cfg | egrep "^[[:space:]]+"

6、显示etc/rc.d/init.d/functions文件中,以#开头,后面跟至少一个空白字符,而后有至少一个非空白字符行

[root@centos7 grub2]# cat /etc/rc.d/init.d/functions | egrep "^#[[:space:]]+[^[:space:]]+"

7、找出netstat -tan命令执行结果中以LISTEN结尾的行。

[root@centos7 grub2]# netstat -tan |egrep "LISTEN[[:space:]]$"
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN
tcp6       0      0 :::111                  :::*                    LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN
tcp6       0      0 ::1:631                 :::*                    LISTEN
tcp6       0      0 ::1:25                  :::*                    LISTEN

8、添加用户bash,testbash,basher,nologin,(shell为/sbin/nologin),而找出当前系统上用户名和默认shell相同的用户。

[root@centos7 grub2]# cat /etc/passwd |egrep "([[:alpha:]]+):.*/\1$"
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
nologin:x:1008:1008::/home/nologin:/sbin/nologin

1.6.2. 练习2-grep

1 、找出ifconfig”网卡名”命令结果中本机的IPv4 地址

[root@centos7 app]# ifconfig ens33 |head -n 2 |tail -n 1 | cut -d " " -f10

2 、查出分区空间使用率的最大百分比值

[root@centos7 app]# df |cut -c 44-46 |sort -n  | tail -n 1
        100

3 、查出用户UID 最大值的用户名、UID 及shell 类型

[root@centos7 app]# cat /etc/passwd  | sort -t ":" -k 3 -nr |head -n 1 |cut -d ":" -f1,3,7
nfsnobody:65534:/sbin/nologin

4 、查出/tmp 的权限,以数字方式显示

[root@centos7 app]#  stat /tmp | head -n 4 |tail -n 1 | cut -d "/" -f1 |cut -d "(" -f2
1777

5 、统计当前连接本机的每个远程主机IP 的连接数,并按从大到小排序

[root@centos7 app]# ss -tn | grep "ESTAB"| tr  -s " "|tr  " " ":"  |cut -d ":" -f6 | sort | uniq -c |sort -r
2 172.18.1.25
[root@localhost ~]# ss -tn  | awk 'BEGIN{ PROCINFO["sorted_in"]="@val_num_desc"} NR >1  {gsub(/:[0-9]+/,"") ; ips[$NF]++;}END{for (ip in ips){print ip,ips[ip]} }'

1.6.3. 练习3-diff,patch

1、获取文件修改改变, 并使用补丁文件恢复文件

[root@centos7 ~]# cd /app
[root@centos7 app]# cp /etc/fstab  f1
cp: overwrite ‘f1’? y
[root@centos7 app]# cp f1 f2
cp: overwrite ‘f2’? y
[root@centos7 app]# echo "add a new line " >> f2
[root@centos7 app]# diff -u f1 f2 >f1-f2.patch
[root@centos7 app]# cat f1
f1           f1-f2.patch  f1.txt
[root@centos7 app]# cat f1-f2.patch
--- f1  2017-11-21 08:16:16.877021441 +0800
+++ f2  2017-11-21 08:16:42.153022966 +0800
@@ -10,3 +10,4 @@
UUID=f4e867e8-bcde-43a2-adc7-c80b0948e85f /app                    ext4    noatime        1 2
UUID=1d6cbe88-ffb4-4adf-bacf-76be1fa75708 /boot                   ext4    defaults        1 2
UUID=b2c064f5-1ee5-4b5c-9e75-ed41cb99c5aa swap                    swap    defaults        0 0
+add a new line
[root@centos7 app]# mv f2 f2.bak
[root@centos7 app]# patch -b f1 f1-f2.patch
bash: patch: command not found...
[root@centos7 app]# rpm -ivh /misc/cd/Packages/pa
[root@centos7 app]# patch -b f1 f1-f2.patch
patching file f1
[root@centos7 app]# cat f1

#
# /etc/fstab
# Created by anaconda on Tue Nov  7 16:07:01 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=59ccea87-3c4e-4bbc-9e2f-3fadb1dcf2e6 /                       ext4    defaults        1 1
UUID=f4e867e8-bcde-43a2-adc7-c80b0948e85f /app                    ext4    noatime        1 2
UUID=1d6cbe88-ffb4-4adf-bacf-76be1fa75708 /boot                   ext4    defaults        1 2
UUID=b2c064f5-1ee5-4b5c-9e75-ed41cb99c5aa swap                    swap    defaults        0 0
add a new line
[root@centos7 app]# cat f1.orig

#
# /etc/fstab
# Created by anaconda on Tue Nov  7 16:07:01 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=59ccea87-3c4e-4bbc-9e2f-3fadb1dcf2e6 /                       ext4    defaults        1 1
UUID=f4e867e8-bcde-43a2-adc7-c80b0948e85f /app                    ext4    noatime        1 2
UUID=1d6cbe88-ffb4-4adf-bacf-76be1fa75708 /boot                   ext4    defaults        1 2
UUID=b2c064f5-1ee5-4b5c-9e75-ed41cb99c5aa swap                    swap    defaults        0 0

1.6.4. 练习4-egrep

1 、显示/proc/meminfo 文件中以大小s 开头的行( 要求:使用两种方法)

[root@centos7 app]# cat /proc/meminfo  |grep -i "^s"
SwapCached:            0 kB
SwapTotal:      134217724 kB
SwapFree:       134217724 kB
Shmem:             26944 kB
Slab:             135480 kB
SReclaimable:      79096 kB
SUnreclaim:        56384 kB
[root@centos7 app]# cat /proc/meminfo  |grep  "^[sS]"
SwapCached:            0 kB
SwapTotal:      134217724 kB
SwapFree:       134217724 kB
Shmem:             26944 kB
Slab:             135480 kB
SReclaimable:      79096 kB
SUnreclaim:        56384 kB

2 、显示/etc/passwd 文件中不以/bin/bash 结尾的行

[root@centos7 app]# cat /etc/passwd |grep -v "/bin/bash$"

3 、显示用户rpc 默认的shell 程序

[root@centos7 app]# cat /etc/passwd |grep "^rpc:" |cut -d ":" -f7
/sbin/nologin

4 、找出/etc/passwd 中的两位或三位数

[root@centos7 app]# cat /etc/passwd |egrep "\b[0-9]{2,3}\b"

5 、显示CentOS7 的/etc/grub2.cfg 文件中,至少以一个空白字符开头的且后面存非空白字符的行

[root@centos7 app]# cat /etc/grub2.cfg | egrep "^[[:space:]]+[^[:space:]]*"

6 、找出“netstat -tan” 命令的结果中以‘LISTEN’ 后跟任意多个空白字符结尾的行

[root@centos7 app]# netstat -tan  |grep "LISTEN[[:space:]]*$"

7 、显示CentOS7 上所有系统用户的用户名和UID

[root@centos7 app]# cat /etc/passwd |grep "^[^:]*:[^:]*:[0-9]{2,3}:.*" |cut -d ":" -f 1,3
[root@localhost ~]# cat /etc/passwd |awk -F ":"  ' $3 < 1000 { print $1 , $ 3}'

8 、添加用户bash 、testbash 、basher 、sh 、nologin( 其shell为/sbin/nologin), 找出/etc/passwd 用户名同shell 名的行

[root@centos7 app]# cat /etc/passwd |egrep "^([^:]*):.*\b\1$"
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
bash:x:1005:1005::/home/bash:/bin/bash
nologin:x:1008:1008::/home/nologin:/sbin/nologin

9 、利用df 和grep取出磁盘各分区利用率,并从大到小排序

[root@centos7 app]# df |grep "[0-9]{1,3}%" -o |grep "[0-9]{1,3}" -o |sort -nr

1.6.5. 练习5-egrep

1 、显示三个用户root 、mage 、wang 的UID 和默认shell

[root@centos7 app]# cat /etc/passwd |grep "(root|mage|wang)" |cut -d ":" -f3,7

2 、找出/etc/rc.d/init.d/functions 文件中行首为某单词(包 包括下划线) 后面跟一个小括号的行

[root@centos7 app]# cat /etc/rc.d/init.d/functions  | egrep "^(_|[[:alpha:]])[[:alnum:]_]*[[:space:]]*\(\)" -o

3 、使用egrep 取出/etc/rc.d/init.d/functions 中其基名

[root@centos7 app]# echo "/etc/rc.d/init.d/function" |egrep -o ".*[^/]+" |egrep "[^/]*$" -o
[root@centos7 app]# echo "/etc/rc.d/init.d/" |egrep -o ".*[^/]+" |egrep "[^/]*$" -o

4 、使用egrep 取出上面路径的目录名

[root@centos7 app]#  echo "/etc/rc.d/init.d/fucntion" | egrep -o ".*[^/]+" |grep ".*/" -o |grep -o ".*[^/]+"
[root@centos7 app]#  echo "/etc/rc.d/init.d/" | egrep -o ".*[^/]+" |grep ".*/" -o |grep -o ".*[^/]+"

5 、统计last 命令中以root 登录的每个主机IP 地址登录次数

[root@centos7 app]# last | grep "^root\b" |cut -d " " -f1,14 |sort -t " " -k2 |uniq -c
17 root 172.18.1.25

6 、利用扩展正则表达式分别表示0-9 、10-99 、100-199、 200-249 、250-255

[root@centos7 app]# echo "123,434,545j4,32432,22,232,32,255" | grep "(([0-9])|([1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]))" -o
123
43
54
32
43
22
232
32
255

7 、显示ifconfig 命令结果中所有IPv4 地址

[root@centos7 app]# ifconfig |egrep "([0-9]|([1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]))(\.([0-9]|([1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]))){3}"  -o
172.18.1.250
255.255.0.0
172.18.255.255
127.0.0.1
255.0.0.0
192.168.122.1
255.255.255.0
192.168.122.255

8 、将此字符串:welcome to magedu linux 中的每个字符去重并排序,重复次数多的排到前面

[root@centos7 app]# echo "welcome to magedu linux"  |grep "[[:alnum:]]"  -o |sort |uniq -c   | sort -nrt " " -k2 |cut -d " " -f8 |tr -d "\n"
[root@localhost ~]# echo "welcome to magedu linux"  | awk -F "" 'BEGIN{PROCINFO["sorted_in"]="@ind_str_asc"}{for (i=1;i<=NF; i++){if($i ~ /[a-zA-Z]/){num[$i]++}}} END{ for (i in num){print i}}'