自学内容网 自学内容网

(九)Shell 脚本(四):正则表达式、sed 和 awk 详解

一、正则表达式

  1. 正则表达式的作用和类型

    • 作用:用于匹配满足特定条件的内容。
    • 类型:分为基础正则表达式和扩展正则表达式。
  2. 正则表达式的区别

    • 基础正则表达式:使用 grep 或者 awk 对数据进行匹配、过滤和显示。
    • 扩展正则表达式:使用 egrep 对数据进行过滤显示。
  3. 正则表达式组成

    • 普通字符:严格区分大小写,包含大小写字母、数字和符号。
    • 元字符:具有特殊含义的符号,如/
  4. 正则表达式匹配的元字符

    • ^:匹配指定字符开始。
    • $:匹配指定字符结束。
    • \:转义字符。
    • *:通配符。
    • ?:匹配任意单个字符。
    • []:匹配方括号内的字符。
    • [^]:匹配除方括号内指定字符以外的字符。
    • [n1 - n2]:匹配指定范围内的字符。
    • {n}:匹配最低次数。
    • {n,m}:匹配最低次数和最高次数。
  5. 扩展正则表达式表示的元字符

    • +:匹配重复一个或者以上的前字符串。
    • ?:匹配零个或者一个字符。
    • |:匹配多个指定字符。
    • ():查找组数据。
    • ()+:匹配组内多次。

二、基础正则和扩展正则表达式使用以及 sed 和 awk 使用

  1. 基础正则表达式 grep 使用

    • grep 选项
      • -n:显示行号。
      • -i:不区分大小写。
      • -v:取反。
    • 匹配指定的字符:使用命令[root@centos01 ~]# grep -n 'the'./test.txt
    • 没有包含 the 的字符的行显示出来:使用命令[root@centos01 ~]# grep -nv 'the'./test.txt
    • 匹配以 sh 括号内包含 i 或者 o 的显示出来:使用命令[root@centos01 ~]# grep -n 'sh[io]'./test.txt
    • 匹配三个 o 或者以上显示出来:使用命令[root@centos01 ~]# grep -n 'ooo'./test.txt
    • 匹配 w 开始两个或者以上 oo 的显示出来:使用命令[root@centos01 ~]# grep -n '[^w]oo'./test.txt
    • 匹配大小写字母开始的行或者数字:使用一系列命令如[root@centos01 ~]# grep -n '[^a-z]oo'./test.txt[root@centos01 ~]# grep -n '[^A-Z]oo'./test.txt[root@centos01 ~]# grep -n '[0-9]'./test.txt[root@centos01 ~]# grep -n '^[A-Z]'./test.txt[root@centos01 ~]# grep -n '^[a-z]'./test.txt
    • grep 符号的使用
      • [root@centos01 ~]# grep -n 'w..d'./test.txt:匹配中间任意两个字符。
      • [root@centos01 ~]# grep -n 'w*d'./test.txt:匹配中间任意字符。
    • 查询{}范围内
      • [root@centos01 ~]# grep -n 'o\{2\}'./test.txt:匹配两个或者以上 o。
      • [root@centos01 ~]# grep -n 'wo\{2,5\}d'./test.txt:匹配两个到五个 o。
  2. 扩展正则表达式使用

    • 查找一次或者多次重复的 o:使用命令[root@centos01 ~]# egrep -n 'wo+d'./test.txt
    • 查找 0 次或者一个前字符内容:使用命令[root@centos01 ~]# egrep -n 'bes?t'./test.txt
    • 匹配包含 so、is、on 的字符显示出来:使用命令[root@centos01 ~]# egrep -n 'so|is|on'./test.txt
    • 匹配括号内包含 a 或者 e 以指定字符开始结束内容显示:使用命令[root@centos01 ~]# egrep -n 't(a|e)st'./test.txt
    • 包含大写 A 和 C 结尾重复多次 xyz 显示出来:使用命令[root@centos01 ~]# egrep -n 'A(xyz)+C'./test.txt
  3. sed

    • sed 工作原理:逐行读取需要处理的数据,根据条件执行命令,然后显示在屏幕上。
    • sed 选项
      • -e:处理文本数据。
      • -f:处理脚本数据。
      • -h:帮助。
      • -n:显示处理后结果。
      • -i:直接修改文本内容。
      • a:添加指定内容。
      • c:替换选定行内容。
      • d:删除。
      • i:指定行插入数据。
      • p:显示处理内容。
      • y:字符转换。
      • s:替换指定字符内容。
    • sed 基本使用显示内容
      • [root@centos01 ~]# sed -n 'p'./test.txt:显示文件所有内容。
      • [root@centos01 ~]# sed -n '5p'./test.txt:显示第五行数据。
      • [root@centos01 ~]# sed -n '1,5p'./test.txt:显示第一行到第五行数据。
      • [root@centos01 ~]# sed -n 'p;n'./test.txt:显示奇数行数据。
      • [root@centos01 ~]# sed -n 'n;p'./test.txt:显示偶数行数据。
      • [root@centos01 ~]# sed -n '1,10{n;p}'./test.txt:显示第一行到第十行的偶数行。
      • [root@centos01 ~]# sed -n '10,${n;p}'./test.txt:显示第十行到文件结尾的偶数行。
      • [root@centos01 ~]# sed -n '4,/the/p'./test.txt:显示第四行后包含一个 the 的内容。
      • [root@centos01 ~]# sed -n '/the/='./test.txt:统计包含 the 关键字的行号。
      • [root@centos01 ~]# sed -n '/^PI/p'./test.txt:匹配大写 PI 开头的行。
      • [root@centos01 ~]# sed -n '/[0-9]/p'./test.txt:匹配包含数字的行。
    • 删除文件内容
      • [root@centos01 ~]# nl test.txt | sed '10d':删除第十行数据并显示。
      • [root@centos01 ~]# nl test.txt | sed '5,10d':删除五到十行数据并显示。
      • [root@centos01 ~]# nl test.txt | sed '/cross/d':删除包含关键字 cross 的行并显示。
      • [root@centos01 ~]# sed '/^[a-z]/d'./test.txt:删除小写字母开头的行。
    • sed 替换数据使用
      • [root@centos01 ~]# sed 's/the/bdqn/'./test.txt:将 the 替换为 bdqn,只替换第一个 the。
      • [root@centos01 ~]# sed 's/l/L/2'./test.txt:将每行的第二个小写 l 替换为大写 L。
      • [root@centos01 ~]# sed 's/the/BDQN/g'./test.txt:将全部小写 the 替换为大写 BDQN。
      • [root@centos01 ~]# sed 's/o//g'./test.txt:删除 o 替换为空。
      • [root@centos01 ~]# sed 's/^/#/'./test.txt:在每行行首添加#号注释行。
      • [root@centos01 ~]# sed '/google/s/^/#/'./test.txt:把 google 开头的行添加注释行。
      • [root@centos01 ~]# sed 's/$/hb3066/'./test.txt:在行尾添加 hb3066 关键字。
      • [root@centos01 ~]# sed '3,10s/the/THE/g'./test.txt:将 3 到 10 行小写 the 替换为 THE。
      • [root@centos01 ~]# sed '/the/s/o/O/g'./test.txt:将包含 the 关键字行的小写 o 替换为大写 O。
    • 迁移内容
      • [root@centos01 ~]# sed '/the/{H;d};$G'./test.txt:将包含 the 关键字的行移动到配置文件末尾。
      • [root@centos01 ~]# sed '1,10{H;d};16G'./test.txt:将 1 到 10 行移动到 16 行以后显示。
      • [root@centos01 ~]# sed '5abdqn'./test.txt:在第五行后添加 bdqn。
      • [root@centos01 ~]# sed '/the/abdqn'./test.txt:在包含 the 行的下一行添加新行输入 bdqn。
      • [root@centos01 ~]# sed '5abdqn\bdqn1'./test.txt:在第五行后换行写入 bdqn 和 bdqn1。
  4. awk 过滤显示文件内容

    • awk 原理:逐行读取显示内容。
    • 选项
      • -f:通过文本或者脚本获取处理数据。
      • -F:分隔符。
    • awk 支持变量类型
      • FS:分割符,默认使用空格表示。
      • NF:处理行的字段数。
      • NR:处理行的行号。
      • $0:处理行的整行数。
      • $n:处理行的第几个字段。
      • RS:数据记录分割,默认\n表示。
    • 过滤显示 passwd 内容以:分隔符,显示 1、3、4 列数据:使用命令[root@centos01 ~]# awk -F ':' '{print $1,$3,$4}' /etc/passwd
    • awk 基本使用
      • [root@centos01 ~]# awk '{print}'./test.txt:显示文件内容。
      • [root@centos01 ~]# awk 'NR==1,NR==5{print}'./test.txt:显示 1 到 5 行。
      • [root@centos01 ~]# awk 'NR==1||NR==5{print}'./test.txt:显示第一行和第五行。
      • [root@centos01 ~]# awk 'NR%2==1{print}'./test.txt:查看奇数行。
      • [root@centos01 ~]# awk 'NR%2==0{print}'./test.txt:查看偶数行。
      • [root@centos01 ~]# awk '/^root/{print}' /etc/passwd:匹配 root 开头的内容。
      • [root@centos01 ~]# awk '/nologin$/{print}' /etc/passwd:过滤禁止登录系统的用户。

原文地址:https://blog.csdn.net/m0_73458595/article/details/142696143

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!