shell中文本的处理工具

1. grep(grep -E = egrep 是等同的效果)

grep 格式:

1).grep 匹配条件 处理文件

命令含义
grep root passwd过滤关键字
grep ^root passwd以 root 开头
grep root$ passwd以 root 结尾

实验前提:

[root@westos_student50 mnt]# cp /etc/passwd .

[root@westos_student50 mnt]# vim passwd

[root@westos_student50 mnt]# grep root passwd   过滤root关键词

[root@westos_student50 mnt]# egrep "bash|sh" passwd  过滤bash和sh关键词

[root@westos_student50 mnt]# grep -E "bash|sh" passwd  过滤bash和sh关键词

参数含义
-i关键字不区分大小写
-E
-数字显示过滤行以及上面和下面几行
-n显示匹配的行所在行号
-A显示过滤行以及下面几行
-B显示过滤行以及上面几行
-v反向过滤
-E “<root” passwdroot 字符之前不能有字符
-E “root>" passwdroot 字符之后不能有字符

 [root@westos_student50 mnt]# vim passwd

[root@westos_student50 mnt]# grep -i root passwd        过滤root时忽略大小写

[root@westos_student50 mnt]# grep -E "\<root" passwd   root字符之前不能有字符

[root@westos_student50 mnt]# grep -E "root\>" passwd   root字符之不能有字符

[root@westos_student50 mnt]# grep -e root -e bash passwd 搜索root和bash和关键词(-e)表示搜索多个

[root@westos_student50 mnt]# grep ROOT passwd  搜索ROOT字符

[root@westos_student50 mnt]# grep -n ROOT passwd 搜索ROOT字符并显示所在行号

[root@westos_student50 mnt]# grep -2 ROOT passwd 搜索ROOT字符并显示上面2行和下面2行

[root@westos_student50 mnt]# grep -A2 ROOT passw 搜索ROOT字符并显示下面2行

[root@westos_student50 mnt]# grep -B2 ROOT passwd搜索ROOT字符并显示上面2行

[root@westos_student50 mnt]# grep ROOT passwd -v 搜索除了ROOT以外的

2).grep 字符数量匹配规则

字符含义
^westos以 westos 开头
westos$以 westos 结尾
w…s以 w 开头 s 结尾,中间有三个字符
…ss 结尾前面有5个字符
*字符出现任意次
0到1次
+1次到任意次
{n}n次
{m,n}m到n次
{0,n}0到n次
{,n}0到n次
{m,}最少 m 次
(lee){2}lee字符串出现2次

 [root@westos_student50 mnt]# vim westos

[root@westos_student50 mnt]# grep ^root passwd 搜索以root开头的关键词

[root@westos_student50 mnt]# grep root$ passwd 搜索以root结尾的关键词

 

[root@westos_student50 mnt]# grep w.s westos w开头s结尾中间1个任意字符

[root@westos_student50 mnt]# grep w..s westos w开头s结尾中间2个任意字符

[root@westos_student50 mnt]# grep w...s westosw开头s结尾中间3个任意字符

[root@westos_student50 mnt]# grep -E  'w.*s' westos 表示过滤以 w 开头,s 结尾中间任意个字符

[root@westos_student50 mnt]# grep -E  'w.?s' westos表示过滤以 w 开头,s 结尾中间0到1

[root@westos_student50 mnt]# grep -E  'w.+s' westos表示过滤以 w 开头,s 结尾中间有1到任意次的字符

[root@westos_student50 mnt]# grep -E  'w.{2}s' westos 表示搜索以w开头s结尾中间有两个字符的

[root@westos_student50 mnt]# grep -E  'w.{,2}s' westos表示过滤以 w 开头s 结尾中间有两个个以下最多两个)字符

[root@westos_student50 mnt]# grep -E  'w.{2,}s' westos表示过滤以 w 开头,s 结尾中间有两个以上字符

[root@westos_student50 mnt]# grep -E  'w.{2,4}s' westos表示过滤以 w 开头,s 结尾中间有两个到四个字符

[root@westos_student50 mnt]# grep -E  'w(ab)+s' westos表示过滤以 w 开头,s 结尾中间ab出现的次数不能少于一次

2. sed

命令格式

sed 参数 命令 处理对象

sed 参数 处理对象 -f 处理规则文件

[root@westos_student50 mnt]# vim passwd

[root@westos_student50 mnt]# sed 5p passwd不加-n会显示全部并将第五行多显示一次

[root@westos_student50 mnt]# sed  -n 5p passwd  =  [root@westos_student50 mnt]# sed -n passwd -f test显示第五行

p显示
sed -n 5p westos   显示第 5 行
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 到最后一行
sed -n ‘/^#/p’ westos显示以#开头的行

[root@westos_student50 mnt]# sed -n 5p passwd显示第五行

[root@westos_student50 mnt]# sed -n 3,5p passwd  显示第三行到第五行

[root@westos_student50 mnt]# sed -n '3p;5p' passwd显示第三行和第五行

[root@westos_student50 mnt]# sed -n '3p;$p' passwd显示第三行和最后一行

[root@westos_student50 mnt]# sed -n '3,$p' passwd显示第三行到最后一行

[root@westos_student50 mnt]# sed -n '/root/p' passwd显示含root的行

[root@westos_student50 mnt]# sed -n '/root/!p' passwd显示除了含root的行

[root@westos_student50 mnt]# sed -n '/^root/p' passwd显示以root开头的行

d删除
sed 5d westos删除第5行
sed ‘/^#/d’ westos把# 开头的行删除
sed ‘/^UUID/!d’ westos除了 UUID 之外的行都删除
sed -e ‘5,$d’ westos删除5到最后一行

[root@westos_student50 mnt]# sed 5d passwd  删除第五行 [root@westos_student50 mnt]# sed '/root/d' passwd 删除包含root的行

[root@westos_student50 mnt]# sed '/root/!d' passwd删除除了包含root的行

 [root@westos_student50 mnt]# sed '1ahello westos' passwd在第一行后面添加hello westos

[root@westos_student50 mnt]# sed '$ahello westos' passwd在最后一行后面添加hello westos [root@westos_student50 mnt]# sed '/lp/ahello westos' passwd 在lp字符之后添加hello westos 

[root@westos_student50 mnt]# sed '1ihello westos' passwd在第一行前插入hello westos [root@westos_student50 mnt]# sed '/lp/ihello westos' passwd在lp字符前插入hello westos

[root@westos_student50 mnt]# sed '/lp/chello westos' passwd将含有lp字符的行改为hello westos 

[root@westos_student50 mnt]# sed '1chello westos' passwd 把第一行改为hello westos

 [root@westos_student50 mnt]# sed '/root/w file' passwd查找含root的行保存到file里面

[root@westos_student50 mnt]# sed '3rtest1' test将test文件与test1文件整合

[root@westos_student50 mnt]# sed '3rtest1' -i test 整合文件后输出到原文件 [root@westos_student50 mnt]# sed 's/sbin/westos/g' passwd全文所有的sbin替换为westos

(s表示列,g表示行)

 [root@westos_student50 mnt]# sed 's/:/###/g' passwd将每一列的:替换为###(加g表示将每一列)

[root@westos_student50 mnt]# sed 's/:/###/' passwd将每一行第一列:替换为### 

[root@westos_student50 mnt]# sed '1,5s/:/###/g' passwd将1到5行每一列的:替换为### [root@westos_student50 mnt]# sed '1s/:/###/g;5s/:/###/g' passwd将第一行和第五行的:替换为### [root@westos_student50 mnt]# sed '$s/:/###/g' passwd替换最后一行的:替换为### [root@westos_student50 mnt]# sed '/lp/,/adm/s/:/###/g' passwd将lp和adm之间的:替换为### 

[root@westos_student50 mnt]# sed '/adm/,/lp/s@/@###@g' passwd将adm和lp之间的/替换为###(@为分割符和上述命令中的/含义相同) 

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                         ##第三列

[root@westos_student50 mnt]# awk -F : 'BEGIN{print "name"}{print $1}END{print "END"}' /etc/passwd将passwd中所有用户的名字捕捉出来 (-F :表示以:为分隔符。print $1表示答应每一行的第一列。"name"表示name字符串,“end”表示end字符串)

[root@westos_student50 mnt]# awk -F : 'BEGIN{N=0}{print $1;N++}END{print N}' /etc/passwd将passwd中所有用户的名字捕捉出来并统计行数 

[root@westos_student50 mnt]# awk '{print NR}' passwd以空格为分隔符显示passwd有多少行

[root@westos_student50 mnt]# awk '{print NF}' passwd以空格为分隔符显示passwd有多少列

[root@westos_student50 mnt]# awk -F : '{print $0}' passwd以为分割符,在当前目录的 passwd 文件中显示所有列内容

[root@westos_student50 mnt]# awk -F : '{print $1}' passwd表示在以为分割符,在当前目录的 passwd 文件中显示第一列内容

[root@westos_student50 mnt]# awk -F : '{print $1,$2}' passwd表示以为分割符,在当前目录的 passwd 文件中显示第一列和第二列内容

[root@westos_student50 mnt]# awk -F : '{print $1,$NF}' passwd表示以为分割符,在当前目录的 passwd 文件中显示第一列和最后一列内容

[root@westos_student50 mnt]# awk -F : '{print $1,$(NF-1)}' passwd表示以为分割符,在当前目录的 passwd 文件中显示第一列和到数第二列内容 [root@westos_student50 mnt]# awk '/root/{print $0}' passwd搜索显示含有root关键字的整行

[root@westos_student50 mnt]# awk '/^root/{print $0}' passwd搜索显示以root关键字开头的整行

[root@westos_student50 mnt]# awk '/root$/{print $0}' passwd搜索显示以root关键字结尾的整行

[root@westos_student50 mnt]# awk '/root/&&/bash$/{print $0}' passwd显示以root开头并且以bash结尾的行

[root@westos_student50 mnt]# awk '/root/||/bash$/{print $0}' passwd显示以root开头或者以bash结尾的行    =    [root@westos_student50 mnt]# awk '/root|bash$/{print $0}' passwd

 [root@westos_student50 mnt]# awk '/^root/||!/nologin$/{print $0}' passwd显示以root开头,或者不是以nologin结尾的行

 [root@westos_student50 mnt]# awk -F : '$6!~/home/{print $0}' passwd显示第六列不带home关键字的

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑 哲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值