文章目录

  • 前言
    • grep (全局搜索正则表达式)
    • sed(stream editor流编辑)
    • awk(报告生成器)
  • 后记

前言

非常重要,shell面试必备

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

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

  • 过滤root关键字:grep root passwd

      [root@rhel7_node1 mnt]# cat passwdtest:root:testtest:test:rootroothahahahahahwestosROOT:testwestoshelloroothellorootabcasdfghjkllkjhgfdsa[root@rhel7_node1 mnt]# grep root passwd test:root:testtest:test:rootroothahahahahahwestoswestoshelloroothellorootabc
    
  • 以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:rootwestoshelloroot
    
  • 忽略大小写:grep -i root passwd

      [root@rhel7_node1 mnt]# grep -i root passwd test:root:testtest:test:rootroothahahahahahwestosROOT:testwestoshelloroothellorootabc
    
  • root字符之前不能有字符:grep -E “<root” passwd

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

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

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

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

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

      [root@rhel7_node1 mnt]# grep -B2 westoslinux passwdmail:x:8:12:mail:/var/spool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologinwestoslinux 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 scriptexit
}
[ -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 avaliableexit}
}
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你了!
反向单引号,之间的东西优先执行

SHELL的文本处理工具相关推荐

  1. Shell程序设计 | 文本处理工具 :正则表达式、grep、sed、awk

    文章目录 正则表达式 grep sed awk 其它常用工具 cut sort uniq tr 常见面试题 第十行 有效电话号码 统计词频 转置文件 要想使用这些流式处理工具,那么就必须得了解正则表达 ...

  2. 史上最全的 Linux Shell 文本处理工具集锦,快收藏!

    点击上方"方志朋",选择"设为星标" 做积极的人,而不是积极废人 来自:大CC 链接:www.cnblogs.com/me15/p/3427319.html 本 ...

  3. 超全的 Linux Shell 文本处理工具集锦,快收藏

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者 | 大CC 来源 | www.cnblogs.co ...

  4. linux shell find depth,搞定 Linux Shell 文本处理工具,看完这篇集锦就够了

    原标题:搞定 Linux Shell 文本处理工具,看完这篇集锦就够了 Linux Shell是一种基本功,由于怪异的语法加之较差的可读性,通常被Python等脚本代替.既然是基本功,那就需要掌握,毕 ...

  5. shell编程之文本处理工具awk

    shell编程之文本处理工具awk 文章目录 shell编程之文本处理工具awk 一.awk介绍 1. awk概述 2. awk能干啥? 二.awk使用方式 1. ==命令行模式使用== ㈠ 语法结构 ...

  6. shell编程之文本处理工具sed

    shell编程之文本处理工具sed 文章目录 shell编程之文本处理工具sed 一.文件编辑器知多少 二.强悍的sed介绍 1. sed用来做啥? 2. sed如何处理文件? 三.sed使用方法介绍 ...

  7. Linux Shell 文本处理工具集锦

    本文将介绍Linux下使用Shell处理文本时最常用的工具: find.grep.xargs.sort.uniq.tr.cut.paste.wc.sed.awk: 提供的例子和参数都是最常用和最为实用 ...

  8. 第二十五期:搞定Linux Shell文本处理工具,看完这篇集锦就够了

    Linux Shell是一种基本功,由于怪异的语法加之较差的可读性,通常被Python等脚本代替.既然是基本功,那就需要掌握,毕竟学习Shell脚本的过程中,还是能了解到很多Linux系统的内容. L ...

  9. 搞定Linux Shell文本处理工具,看完这篇集锦就够了(转)

    Linux Shell是一种基本功,由于怪异的语法加之较差的可读性,通常被Python等脚本代替.既然是基本功,那就需要掌握,毕竟学习Shell脚本的过程中,还是能了解到很多Linux系统的内容. L ...

最新文章

  1. 旷视孙剑博士提出LGD,训练速度提升51%,适用于目标检测的知识蒸馏
  2. 高效代码审查:来自前质疑者的9个建议
  3. Python列表排序 reverse、sort、sorted 操作方法详解
  4. 清华博导:我有个好学生想放弃科研 去中学当老师
  5. jQuery源码解读二(apply和call)
  6. fopen文件路径怎么写_php的多功能文件操作类
  7. 蒙文字体怎么安装_我们来聊一聊iOS13的“字体”该怎么用?
  8. 顺序链表的C风格实现
  9. robo3t怎么插入数据_robo 3T(robomongo)在ubuntu16.04上安装记录
  10. python+opencv代码给证件照换底色
  11. 用Python写一个企业微信自动化打卡的脚本
  12. 读书笔记--项亮《推荐系统实践》第四章
  13. 无法加载文件 C:\Users\haoqi\Documents\WindowsPowerShell\profile.ps1,因为在此系统上禁止运行脚本
  14. xml解析方式与效率对比
  15. 结对编程的合作情况,以及结对编程的优缺点
  16. 使用R语言绘制graph:无向图(ug)和有向无环图(dag)
  17. supervisor中使用虚拟环境
  18. 汇总一下Intellij IDEA炫酷的插件
  19. C语言:使用函数计算一个数的阶乘
  20. shell 对字符串去重并排序

热门文章

  1. 基于 Python 的地理空间绘图指南
  2. 【论文阅读】[CVPR 2018] PU-Net: Point Cloud Upsampling Network. [tensorflow] [rec. oth.]
  3. Unity Shader - GrabPass 实现武器热扭曲拖尾效果
  4. 微信开发公众号本地调试
  5. 开发一个类似美团的外卖小程序多少钱
  6. 【论文复现,含代码】MatchNet: Unifying Feature and Metric Learning for Patch-Based Matching
  7. 生成数据库结构字段SQL语句
  8. 如何深入学习 Android Framework
  9. cx_Freez打包Python批处理
  10. 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。