运维(SRE)成长之路-第3天 文本处理三剑客之 grep

 1.命令作用以及参数详解

 grep: 全局搜索正则表达式并打印行(Global search REgular expression and Print out the line)作用:文本搜索工具,根据用户指定的“模式”对目标文本逐行进行匹配检查;打印匹配到的行模式:由正则表达式字符及文本字符所编写的过滤条件
  格式:

grep [OPTIONS] PATTERN [FILE...]

常见选项:
–color=auto 对匹配到的文本着色显示
-m # 匹配#次后停止
-v 显示不被pattern匹配到的行
-i 忽略字符大小写
-n 显示匹配的行号
-c 统计匹配的行数
-o 仅显示匹配到的字符串
-q 静默模式,不输出任何信息
-A # after, 后#行
-B # before, 前#行
-C # context, 前后各#行
-e 实现多个选项间的逻辑or关系,如:grep –e ‘cat ’ -e ‘dog’ file
-w 匹配整个单词
-E 使用ERE,相当于egrep
-F 不支持正则表达式,相当于fgrep
-f file 根据模式文件处理
-r 递归目录,但不处理软链接
-R 递归目录,但处理软链接

2.案例解析

范例:

grep root /etc/passwd
grep "USER"  /etc/passwd
grep 'USER'  /etc/passwd
grep whoami  /etc/passwd

范例:取两个文件的相同行

[root@centos8 ~]#cat /data/f1.txt
a
b
1
c
[root@centos8 ~]#cat /data/f2.txt
b
e
f
c
1
2
[root@centos8 ~]#grep -f /data/f1.txt  /data/f2.txt
b
c
1

范例:

df | grep '^/dev/sd' |tr -s ' ' %|cut -d% -f5|sort -n|tail -1

范例:

[root@centos8 ~]#ss -nt | grep "^ESTAB" |tr -s ' ' : |cut -d: -f6|sort |uniq -c|sort -nr|head -n3
      3 10.0.0.1
      1 172.16.4.100
      1 172.16.31.188

范例:

[root@centos8 ~]#grep -v "^#" /etc/profile | grep -v '^$'
[root@centos8 ~]#grep -v "^#\|^$" /etc/profile
[root@centos8 ~]#grep -v "^\(#\|$\)" /etc/profile 
[root@centos8 ~]#grep -Ev "^(#|$)" /etc/profile
[root@centos8 ~]#egrep -v "^(#|$)" /etc/profile
[root@centos6 ~]#egrep -v '^(#|$)' /etc/httpd/conf/httpd.conf

范例:

[root@centos8 ~]#grep -o  'r..t' /etc/passwd
root
root
root
root
r/ft
rypt

范例:

[root@centos8 ~]#ifconfig | grep -E '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}'
        inet 10.0.0.8  netmask 255.255.255.0  broadcast 10.0.0.255
        inet 172.16.0.123  netmask 255.255.0.0  broadcast 172.16.255.255
        inet6 fe80::c11e:4792:7e77:12a4  prefixlen 64  scopeid 0x20
        inet 127.0.0.1  netmask 255.0.0.0
[root@centos8 ~]#ifconfig | grep -E '([0-9]{1,3}.){3}[0-9]{1,3}'
        inet 10.0.0.8  netmask 255.255.255.0  broadcast 10.0.0.255
        inet 172.16.0.123  netmask 255.255.0.0  broadcast 172.16.255.255
        inet6 fe80::c11e:4792:7e77:12a4  prefixlen 64  scopeid 0x20
        inet 127.0.0.1  netmask 255.0.0.0

[root@centos8 ~]#ifconfig eth0 | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}'|head -1
10.0.0.8

[root@centos8 ~]#cat regex.txt 
([0-9]{1,3}\.){3}[0-9]{1,3}
[root@centos8 ~]#ifconfig | grep -oEf regex.txt 
10.0.0.8
255.255.255.0
10.0.0.255
127.0.0.1
255.0.0.0

范例:

[root@centos8 ~]#grep -E 'root|bash' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
wang:x:1000:1000:wang:/home/wang:/bin/bash
mage:x:1001:1001::/home/mage:/bin/bash
xiaoming:x:1002:1002::/home/xiaoming:/bin/bash
roob:x:1003:1003::/home/roob:/bin/bash
[root@centos8 ~]#grep -e 'root' -e 'bash' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
wang:x:1000:1000:wang:/home/wang:/bin/bash
mage:x:1001:1001::/home/mage:/bin/bash
xiaoming:x:1002:1002::/home/xiaoming:/b

范例:

[root@centos8 ~]#grep -w root  /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@centos8 ~]#grep  '\'  /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

范例:

[root@centos8 ~]#grep "^\(.*\)\>.*\<\1$" /etc/passwd
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:1008:1008::/home/bash:/bin/bash
nologin:x:1011:1011::/home/nologin:/sbin/nologin

[root@centos8 ~]#grep -E "^(.*)\>.*\<\1$" /etc/passwd
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:1008:1008::/home/bash:/bin/bash
nologin:x:1011:1011::/home/nologin:/sbin/nologin

[root@centos8 ~]#egrep  "^(.*)\>.*\<\1$" /etc/passwd
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:1008:1008::/home/bash:/bin/bash
nologin:x:1011:1011::/home/nologin:/sbin/nologin

范例:面试题,算出所有人的年龄总和

[root@centos8 ~]#cat /data/nianling.txt
xiaoming=20
xiaohong=18
xiaoqiang=22

[root@centos8 ~]#cut -d"=" -f2 /data/nianling.txt|tr '\n' + | grep -Eo ".*[0-9]"|bc
60
[root@centos8 ~]#grep -Eo "[0-9]+" /data/nianling.txt | tr '\n' + | grep -Eo ".*[0-9]"|bc
60

3.结尾(~有彩蛋~)

 为了让乏味的日常不再那么枯燥,给大家准备了一个花活儿 (渲染爱心),如果你感兴趣的话,我把具体的脚本展示在下方。 

#!/bin/bash
y=1250
for (( yy = 45; yy > 0; yy-- )); do
x=-1140
        for (( xx = -120; xx < 0; xx++ )); do
                ff=$(echo `awk -v x=${x} -v y=${y} 'BEGIN{printf "%.0f\n",(((x/1000)*(x/1000)+(y/1000)*(y/1000)-1)*((x/1000)*(x/1000)+(y/1000)*(y/1000)-1)*((x/1000)*(x/1000)+(y/1000)*(y/1000)-1)-(x/1000)*(x/1000)*(y/1000)*(y/1000)*(y/1000))*10000000}'`)
                if [[ ff -le 0 ]]; then
                        printf "\e[1;41m \e[0m"
                else
                    printf " "
                fi
        x=$((${x}+20))
        done
printf "\n"
y=$((${y}-50))
done

你可能感兴趣的:(#,Linux基础,运维,数学建模,linux)