文本处理三剑客
一、grep
1.grep格式
grep 文本搜索(Globally search a Regular Expression and Print)
准备实验素材: cp /etc/passwd . vim passwd vim passwd 
grep
grep -E = egrep
##grep 格式##
grep 匹配条件 处理文件
grep root passwd #过滤root关键字
grep -i root passwd ##后略大小写
grep -E "\<root" passwd ##root字符之前不能有字符
grep -E "root\>" passwd ##root字符之后不能有字符
grep -数字 ##显示过滤行以及上面几行和下面几行
grep -n ##显示匹配的行所在行号
grep -A ##显示过滤行以及下面几行
grep -B ##显示过滤行以及上面几行
grep -v ##反向过滤
- grep -E 以及 egrep
 2.精确搜索某一字符 grep -E “<root” passwd ##root字符之前不能有字符 grep -E “root>” passwd ##root字符之后不能有字符
??当前grep是一种强大的文本搜索工具。我们可以通过grep命令指定特定搜索条件来搜索文件及其内容以获取有用的信息。grep是全局搜索正则表达式并打印出匹配的行,其抓取数据是贪婪模式,即不会漏掉过滤内容,但准确性会相对降低。   3.关于显示行数的用法 
2.grep字符数量匹配规则
正则表达式 含义
^westos 以westos关键字开头
westos$ 以westos结尾
w…s w开头s结尾中间含有3个任意字符
…s s结尾前面有3个任意字符
* 字符出现次数任意(即0次到任意次)
? 字符出现0到1次
+ 字符出现1次到任意次
{n} 字符出现n次
{m,n} 字符出现m到n次
{,n} 字符出现0到n次
{m,} 字符最少出现m次
(lee){2} ##lee字符串出现2次
创建实验素材 
 
3.测试
测试:请显示系统中能被su命令切换的用户名称: ? 首先我们回顾用户的类型 ? 1.linux系统中用户分为三类: 第一类:root(超级管理员),UID为0,这个用户有极大的权限,可以直接无视很多的限制,包括读写执行的权限。 第二类:系统用户,UID为1~499。一般是不会被登入的。 第三类就是普通用户,UID范围一般是500~65534。这类用户的权限会受到基本权限的限制,也会受到来自管理员的限制。 ? 2.su 是最简单的用户切换命令,通过该命令可以实现任何身份的切换,包括从普通用户切换为 root 用户、从 root 用户切换为普通用户以及普通用户之间的切换。 ? 3.涉及到用户,就离不开用户身份信息文件 即:/etc/passwd ,从该文件中可以看出 普通用户及超级用户都有一个特点 那就是以bash或sh结尾。 ? 综上,我们可以得出结果: 
二、sed
sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换、删除、新增、选取
**注意:**sed 默认不会直接修改源文件数据,而是会将数据复制到缓冲区(模式空间)中,修改也仅限于缓冲区中的数据。 也就是说 真是文件在硬盘里面,sed修改的是内存里的。
命令格式:
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 ##显示以#开头的行
1. -n的含义
首先,-n的意思是  默认情况下,sed将在每个周期结束时通过脚本打印出模式空间。这个选项禁用这种自动打印,sed只有在通过p命令明确告知时才会产生输出。

2.sed之-p的用法
p就是显示。 -f是指定策略文件 sed根据提供的规则命令匹配并修改数据
 准备实验素材:  使用:    
3.sed之-d的用法
d ##删除
sed 5d westos ##删除第五行
sed '/^#/d' fstab ##把#开头的行删除
sed '/^UUID/!d' fstab ##除了UUID以外的行都删除
sed -e '5,$d' westos
  
4.sed之-a的用法
a ##添加
sed '$a hello world' fstab ## 在最后一行后面加上hello westos
sed '$a hello\nworld' fstab ## 在最后一行后面加上hello westos (输出格式为分行输出 如下图所示)
sed '/^#/a hello world' fstab ## 所有以#开头的行 后面加上hello westos
 
5.sed之-i的用法
i ##插入
sed '5ihello westos' westos
 
6.sed之-c的用法
c ##替换
sed -e '/^#/c hello world' fstab
sed '5chello world' westos

7.sed用法之-w的用法
w ##把符合的行写到指定文件中
sed '/^UUID/w westofile' westos ##把westos中UUID开头的行写入westosfile中

8.sed之-r的用法
r ##整合文件
sed '5r haha' westos
 现在 我们用sed命令对其做出操作: 
练习及脚本:Apache_port.sh 此脚本接入数字,http的端口就改为此数字,假设selinux为关闭状态 例如: sh Apache_port.sh ERROR: Pleaase input port number following script !! sh Apache_port.sh 8080 apache的端口会被修改为8080 ? 一共分三步: 1.检测脚本命令后是否跟数字 2.检测httpd是否安装 3.检测端口是否被占用 ? 
sed 字符替换
sed 's/:/###/g' westos
sed 's/:/###/' westos
sed 's/:/###/g' westos
sed '1,5s/:/###/g' westos
sed '1s/:/###/g' westos
sed '1s/:/###/g;5s/:/###/g' westos
sed '/lp/,/shutdown/s/:/###/g' westos
sed 's/\//####/g' westos
sed 's@/@####@g' westos
sed 's@/@####@g' -i westos 把sed处理的内容保存到westos文件中
  
 
三、awk
awk是行处理器: 相比较屏幕处理的优点,在处理庞大文件时不会出现内存溢出或是处理缓慢的问题,通常用来格式化文本信息 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 #第三列

   
   
|