文本处理工具


1 grep

  • 全称: [Globally search a Reguler Expression and Print]
  • grep -Eegrep 等同的
  • grep格式
  • grep + 匹配条件 + 处理文件
cd /mnt
vim passwd #将/etc/passwd中的部分内容放入passwd中

使用grep命令编写一个shell脚本 shell grep -n_apache

grep root passwd #过滤root关键字


使用grep命令编写一个shell脚本 shell grep -n_使用grep命令编写一个shell脚本_02


grep ^root passwd #以root开头


使用grep命令编写一个shell脚本 shell grep -n_运维_03


grep root$ passwd #以root结尾


使用grep命令编写一个shell脚本 shell grep -n_apache_04


grep -i root passwd #忽略大小写


使用grep命令编写一个shell脚本 shell grep -n_sed_05


grep -E “<root” passwd #root字符之前不能有字符


使用grep命令编写一个shell脚本 shell grep -n_运维_06


grep -E “root>” passwd #root字符之后不能有字符


使用grep命令编写一个shell脚本 shell grep -n_使用grep命令编写一个shell脚本_07


grep -数字 #显示过滤行以及上面几行和下面几行


使用grep命令编写一个shell脚本 shell grep -n_linux_08


grep -n #显示匹配的行所在行号


使用grep命令编写一个shell脚本 shell grep -n_运维_09


grep -A #显示过滤行以及下面几行


grep -B #显示过滤行以及上面几行


使用grep命令编写一个shell脚本 shell grep -n_linux_10


grep -v #反向过滤


使用grep命令编写一个shell脚本 shell grep -n_apache_11

  • grep字符数量匹配规则
  • ^root #以westos开头

in$ #以westos结尾

使用grep命令编写一个shell脚本 shell grep -n_linux_12

s…n #s开头n结尾中间2个字符

使用grep命令编写一个shell脚本 shell grep -n_linux_13

…n #n结尾前面3个字符

使用grep命令编写一个shell脚本 shell grep -n_apache_14

使用grep命令编写一个shell脚本 shell grep -n_linux_15

使用grep命令编写一个shell脚本 shell grep -n_使用grep命令编写一个shell脚本_16

  • {n} #n次
  • {m,n} #m到n次
  • {0,n} #0-n次
  • {,n} #0-n次
  • {m,} #最少m次
  • (lxy){2} #lxy字符串出现2次
  • 练习
    请显示系统中能被su命令切换的用户名称
  • 使用grep命令编写一个shell脚本 shell grep -n_使用grep命令编写一个shell脚本_17


2 sed

  • 命令格式
  • sed 参数 命令 处理对象
  • sed 参数 处理对象 -f 处理规则文件
  • 对字符的处理
    p #显示
    sed -n 5p westos #显示第五行
    sed -n 3,5p westos #显示三到五行
    sed -ne “3p;5p” westos #显示3和5行
    sed -ne 1,5p westos #1-5行
    sed -ne ‘5,$p’ westos #5到最后一行
    sed -n ‘/^#/p’ fstab #显示以#开头的行
  • 使用grep命令编写一个shell脚本 shell grep -n_使用grep命令编写一个shell脚本_18


  • 使用grep命令编写一个shell脚本 shell grep -n_运维_19


  • 使用grep命令编写一个shell脚本 shell grep -n_使用grep命令编写一个shell脚本_20


  • 使用grep命令编写一个shell脚本 shell grep -n_linux_21


  • 使用grep命令编写一个shell脚本 shell grep -n_使用grep命令编写一个shell脚本_22

    使用grep命令编写一个shell脚本 shell grep -n_运维_23

  • d #删除
    sed 5d westos #删除第五行
    sed ‘/^#/d’ fstab #把#开头行删除
    sed ‘/^UUID/!d’ fstab #除了UUID以外的行都删除
    sed -e ‘5,$d’ westos
  • 使用grep命令编写一个shell脚本 shell grep -n_sed_24

    使用grep命令编写一个shell脚本 shell grep -n_apache_25

  • a #添加
    sed -e ’ $ a hello world’ fstab
    sed -e ‘$ a hello\nworld’ fstab
    sed -e ‘/^#/a hello world’ fstab
  • 使用grep命令编写一个shell脚本 shell grep -n_sed_26

    使用grep命令编写一个shell脚本 shell grep -n_linux_27

  • c #替换
    sed -e ‘/^#/c hello world’ fstab
    sed ‘5chello world’ westos
  • 使用grep命令编写一个shell脚本 shell grep -n_linux_28

    使用grep命令编写一个shell脚本 shell grep -n_运维_29

  • w #把符号的行写到指定文件中
    sed ‘/^UUID/w westosfile’ westos #把westos中UUID开头的行写入westosfile中
  • 使用grep命令编写一个shell脚本 shell grep -n_运维_30

  • i #插入
    sed ‘5ihello westos’ westos
  • 使用grep命令编写一个shell脚本 shell grep -n_使用grep命令编写一个shell脚本_31

  • r #整合文件
    sed ‘5r haha’ westos
  • 使用grep命令编写一个shell脚本 shell grep -n_sed_32

sed字符替换

sed ‘s/ : /###/g’ westos

使用grep命令编写一个shell脚本 shell grep -n_linux_33

sed ‘s/ : /###/’ westos

使用grep命令编写一个shell脚本 shell grep -n_运维_34

sed ‘s/ : /###/g’ westos

使用grep命令编写一个shell脚本 shell grep -n_使用grep命令编写一个shell脚本_35

sed ‘1,5s/ : /###/g’ westos

使用grep命令编写一个shell脚本 shell grep -n_使用grep命令编写一个shell脚本_36

sed ‘1s/ : /###/g’ westos

使用grep命令编写一个shell脚本 shell grep -n_sed_37

sed ‘1s/ : /###/g;5s/ : /###/g’ westos

使用grep命令编写一个shell脚本 shell grep -n_apache_38

sed ‘s/ \ //####/g’ westos

使用grep命令编写一个shell脚本 shell grep -n_linux_39

sed ‘s@/@####@g’ westos
sed ‘s@/@####@g’ -i westos 把sed处理的内容保存到westos文件中

  • 练习及脚本
    Apache_port.sh
    此脚本后介入数字
    http的端口就改为此数字
    假设selinux为关闭状态

使用grep命令编写一个shell脚本 shell grep -n_运维_40

使用grep命令编写一个shell脚本 shell grep -n_linux_41


3 awk

awk -F 分隔符 BEGIN{}{}END{} FILENAME
NR #行数
NF #列数
FILENAME #文件名称本身
westos #westos变量值
“westos” #westos字符串/bash$/ #条件
/条件1|条件2/ #条件1或者条件2
/条件1/||/条件2/ #条件1或者条件2
/条件1/&&/条件2/ #条件1并且条件2$0 #所有的列
$1 #第一列
$2 #第二列
$3 #第三列#/etc/passwd文件的第六列没有home关键字并且以bash结尾的行
 awk -F : ‘KaTeX parse error: Expected ‘EOF’, got ‘&’ at position 10: 6!~/home/&̲&/bash/{print}’ /etc/passwd

使用grep命令编写一个shell脚本 shell grep -n_linux_42