SHELL的文本处理工具

前言

非常重要,shell面试必备

grep (全局搜索正则表达式)

grep(globle regular expression print)
grep -E = egrep(grep的扩展)
grep 格式:(贪婪模式)
grep 匹配条件 处理文件

  • 过滤root关键字:grep root passwd

      [root@rhel7_node1 mnt]# cat passwd
      test:root:test
      test:test:root
      roothahahahahahwestos
      ROOT:test
      westoshelloroot
      hellorootabc
      asdfghjkllkjhgfdsa
      [root@rhel7_node1 mnt]# grep root passwd 
      test:root:test
      test:test:root
      roothahahahahahwestos
      westoshelloroot
      hellorootabc
    
  • 以root开头:grep ^root passwd

      [root@rhel7_node1 mnt]# grep ^root passwd 
      roothahahahahahwestos
    
  • 以root结尾:grep root$ passwd

      [root@rhel7_node1 mnt]# grep root$ passwd 
      test:test:root
      westoshelloroot
    
  • 忽略大小写:grep -i root passwd

      [root@rhel7_node1 mnt]# grep -i root passwd 
      test:root:test
      test:test:root
      roothahahahahahwestos
      ROOT:test
      westoshelloroot
      hellorootabc
    
  • root字符之前不能有字符:grep -E “<root” passwd

      [root@rhel7_node1 mnt]# grep -E "\<root" passwd 
      test:root:test
      test:test:root
      roothahahahahahwestos
    
  • root字符之后不能有字符:grep -E “root>” passwd

      [root@rhel7_node1 mnt]# grep -E "root\>" passwd 
      test:root:test
      test:test:root
      westoshelloroot
    
  • 显示过滤行以及上面n行和下面n行:grep -数字n

      [root@rhel7_node1 mnt]# grep -2 westoslinux passwd
      mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
      operator:x:11:0:operator:/root:/sbin/nologin
      westoslinux test
      games:x:12:100:games:/usr/games:/sbin/nologin
      ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    
  • 显示匹配的行所在行号:grep -n

      [root@rhel7_node1 mnt]# grep -n westoslinux passwd
      11:westoslinux test
    
  • 显示过滤行以及下面几行:grep -A数字

       [root@rhel7_node1 mnt]# grep -A2 westoslinux passwd
      westoslinux test
      games:x:12:100:games:/usr/games:/sbin/nologin
      ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    
  • 显示过滤行以及上面几行:grep -B数字

      [root@rhel7_node1 mnt]# grep -B2 westoslinux passwd
      mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
      operator:x:11:0:operator:/root:/sbin/nologin
      westoslinux test
    
  • 反向过滤:grep -v

grep字符数量匹配规则:

  • 以westos开头:^westos

      grep -E "^westos" 文件名
    
  • 以westos结尾:westos$

  • w开头s结尾中间4个任意字符:w…s

  • s结尾前面5个任意字符:…s

  • 字符出现0到任意次:*

  • 1到任意次:+

  • 0到1次:?

  • n次:{n}

  • m到n次:{m,n}

  • 0到n次:{0,n}

  • 0到n次:{,n}

  • 最少m次:{m,}

  • hai字符串出现2次:(hai){2}

练习脚本:
请显示系统中能被su命令切换的用户名称

[root@rhel7_node1 etc]# vim show_loginuser.sh
#!/bin/bash
grep -E "bash$|sh$|tsh$|csh$" /etc/passwd | cut -d : -f 1

测试:

[root@rhel7_node1 etc]# sh show_loginuser.sh 
root
westos
westos1
westos2

sed(stream editor流编辑)

命令格式:
sed 参数 命令 处理对象
sed 参数 -f 处理规则文件 处理对象

对字符的处理

  • 显示:p
    sed -n 5p westos (显示第五行)
    sed -n 3,5p westos (显示3到5行)
    sed -ne “3p;5p” westos (显示3和5行)
    sed -ne 1,5p westos (1-5行)
    sed -ne ‘5,$p’ westos (5到最后以行)
    sed -n ‘/^#/p’ fstab (显示以#开头的行)
  • 删除:d
    sed 5d westos (删除第五行)
    sed ‘/^#/d’ fstab (把#开头的行删除)
    sed ‘/^UUID/!d’ fstab (除了UUID以外的行都删除)
    sed -e ‘5,$d’ westos(删除5行及其之后文件)
  • 添加:a
    sed -e ‘$a hello world’ fstab(最后一行添加)
    sed -e ‘$a hello\nworld’ fstab(添加且换行)
    sed -e ‘/^#/a hello world’ fstab(在以#开头的后面添加)
  • 替换:c
    sed -e ‘/^#/c hello world’ fstab(以#号开头的都替换)
    sed ‘5chello world’ westos(将第5行替换)
  • 把符合的行写到指定文件中:w
    sed ‘/^UUID/w westofile’ westos(把westos中UUID开头的行写入westosfile中)
  • 插入:i
    sed ‘5i hello westos’ westos(插入到第五行上面)
  • 整合文件:r
    sed ‘5r haha’ westos(将haha整合到westos文件第5行下面)

sed 字符批量替换

	sed 's/:/###/g' westos(全文范围把:替换为###)
	sed 's/:/###/' westos(每行第一个替换)
	sed '1,5s/:/###/g' westos(第一行到第五行)
	sed '1s/:/###/g;5s/:/###/g' westos(第一行和第五行)
	sed '/lp/,/shutdown/s/:/###/g' westos(lp到shutdown之间的)
	sed 's/\//####/g' westos(把/替换掉得转义这个)
	sed 's@/@####@g' westos(分隔符可以用@表示)
	保存更改内容:
	sed 's@/@####@g' -i westos (把sed处理的内容保存到westos文件中)

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

脚本设置:

#!/bin/bash
[ -z "$1" ] && {
        echo Error: not port number Please give port following script
        exit
}
[ -z "`netstat -antlupe | grep $1`" ] || {   
        echo Error:$1 is used by system proto!
        exit
}
[ -e "/etc/httpd/conf/httpd.conf" ] || {
        yum install httpd -y &> /dev/null || {
                echo apache not installed and yum repo is not avaliable
                exit
        }
}
sed "/^Listen/c Listen $1" -i /etc/httpd/conf/httpd.conf
systemctl restart httpd  > /dev/null &&{
        echo "Configure port sucessfully!!"
} ||{
        echo "Error: can't up service!"
}

测试:

[root@rhel7_node1 mnt]# sh  apache_port.sh 2227
Configure port sucessfully!!

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

课后练习
统计如何在系统中能su切换的并且用户加目录不在/home下的用户数量

答案:

awk -F : 'BEGIN{N=0}$6!~/home/&&/bash$|csh$|tcsh$/{print $1;N++}END{print N}' /etc/passwd

后记

练习and补进度
ballball你了!
反向单引号,之间的东西优先执行

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章