文本处理三剑客(grep、awk、sed)

文本处理三剑客简介(grep、awk、sed)

awk 支持所有的正则表达式
sed 默认不支持扩展表达式,加-r 选项开启 ERE,如果不加-r 使用花括号要加转义符{}
grep 默认不支持扩展表达式,加-E 选项开启 ERE,如果不加-E 使用花括号要加转义符{}
egrep 支持基础和扩展表达式

awk

awk不仅仅时linux系统中的一个命令,而且是一种编程语言,可以用来处理数据和生成报告(excel)。处理的数据可以是一个或多个文件,可以是来自标准输入,也可以通过管道符获取标准输入,awk可以在命令行上直接编辑命令进行操作,也可以编写成awk程序来进行更为复杂的运用。三剑客老大!

1、awk

awk [option] ‘pattern {action}’ filename

命令 + 选项 + 找谁 + 干啥 + 文件名
option:
-F :指定分隔符(不指定默认以空格为分隔符)

实例:
awk ‘NR>=2&&NR<=5{print $0}’ test
bin❌1:1:bin:/bin:/sbin/nologin
daemon❌2:2:daemon:/sbin:/sbin/nologin
i#adm❌3:4:adm:/var/adm:/sbin/nologin
lp❌4:7:lp:/var/spool/lpd:/sbin/nologin
分析awk处理过程:

读取文本第一行,首先会和匹配模式进行相匹配,如果发现第一行内容不和模式相匹配的话,就继续读取下一行
读取到第二行发现和模式相匹配,就会执行花括号里面的动作
然后继续读取下一行,发现和模式相匹配,就会执行花括号里面的动作

依次读取全文

2、awk基本概念及内置匹配变量

基本概念:

概念 属性
记录(record) 一行就是一个记录
分隔符(field separator) 进行对记录进行切割的时候所使用的字符
字段(field) 将一条记录分割成的每一段
FILENAME 当前处理文件的文件名
FS(Field Separator) 字段分隔符(默认是以空格为分隔符=)
NR(Number of Rrecord) 记录的编号(awk每读取一行,NR就加1==)
NF(Number of Field) 字段数量(记录了当前这条记录包含多少个字段==)
ORS(Output Record Separator) 指定输出记录分隔符
(指定在输出结果中记录末尾是什么,默认是\n,也就是换行)
OFS(Output Field Separator) 输出字段分隔符(默认值是一个空格)
RS 记录分隔符(默认是一个换行符)
内置匹配变量:

变量名 属性
$1 2 … 2 … 2n 当前记录的第n个字段,字段间由FS分隔
$NF 输出最后一个字段
$0 输出整条记录

3、awk运算符

运算符 描述
= += -= *= /= %= ^= **= 赋值
|| 逻辑或
&& 逻辑与
~ ~! 匹配正则表达式和不匹配正则表达式
< <= > >= != == 关系运算符

    • 加,减
  • / & 乘,除与求余
    • ! 一元加,减和逻辑非
      ^ *** 求幂
      ++ – 增加或减少,作为前缀或后缀

4、awk-正则

基础正则:

符号 描述
. 匹配任意单个字符(必须存在)
^ 匹配以某个字符开头的行
$ 配以什么字符结尾的行

  • 匹配前面的一个字符出现0次或者多次;eg:ab
    .
    表示任意长度的任意字符
    [] 表示匹配括号内的一个字符
    [^] 匹配[^字符]之外的任意一个字符
    [] 匹配非[^字符]内字符开头的行
    < 锚定 单词首部;eg:<root
    > 锚定 单词尾部:eg:>root

扩展正则:
符号 描述

  • 表示前面的字符至少出现1次的情况
    | 表示“或”
    ? 表示前面的字符至多出现1次的情况

实例1:打印出来以root开头的行
awk ‘/^root/{print $0}’ test
root❌0:0:root:/root:/bin/bash
awk '/^root/'test
root❌0:0:root:/root:/bin/bash

实例2:第五个字段包含root的行
第五个字段包含root 打印出来( “~ ” 正则匹配)
awk -F “:” ‘$5~/root/{print $0}’ test
root❌0:0:root:/root:/bin/bash

实例3:打印本机ip地址
ip a|grep global|awk -F " +|/" ‘{print $3}’
192.168.137.6

ip a|awk -F " +|/" ‘$0~/global/{print $3}’
192.168.137.6

5、awk完整模式-BEGIN模式与END模式

语法(awk基本结构):

awk BEGIN{coms} /pattern/{coms} END{coms}

awk + 开始模块 + 找谁干啥 + 结束模块
awk数组

arrayname[string]=value
数组名[元素名]=值

实例1:
cat test
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
daemon❌2:2:daemon:/sbin:/sbin/nologin
i#adm❌3:4:adm:/var/adm:/sbin/nologin
lp❌4:7:lp:/var/spool/lpd:/sbin/nologin
shutdown❌6:0:shutdown:/sbin:/sbin/shutdown
halt❌7:0:halt:/sbin:/sbin/halt
mail❌8:12:mail:/var/spool/mail:/sbin/nologin
operator❌11:0:operator:/root:/sbin/nologin

awk ‘BEGIN{i=0}/root/{i++}END{print i}’ test
2
awk ‘/root/{i++}END{print i}’ test
2

实例2:统计访问的网址
#用cut+sort+uniq的方法
cat test | cut -d “/” -f 3 | sort | uniq -c | sort -rn
25 www.taobao.com
13 www.sina.com
7 www.qq.com

#用awk+sort的方法
cat test | awk -F “/+” ‘{du[$2]++}END{ for ( i in du) print du[i],i}’ | sort -rn
25 www.taobao.com
13 www.sina.com
7 www.qq.com

##分析awk都干了些什么?
#第一步:awk一行一行读取test文件,切割出访问域名eg:www.qq.com
#定义一个数组:数组名为du 元素名为访问域名
#每行读取到相同元素名,值+1 例:du[www.qq.com]+=1
#第二步:END模块 for循环输出数组;

sed工作原理

sed读取一行,首先将这行放入到缓存中
然后,才对这行进行处理
处理完成以后,将缓冲区的内容发送到终端
存储sed读取到的内容的缓存区空间称之为:模式空间(Pattern Space)

sed选项
option:
选项 解释说明
-n(no) 取消默认的sed的输出,常与p连用
-e(entry) 多点操作
-r(ruguler) 使用使用扩展正则表达式,默认sed只识别基本正则表达式
-i(inside) 直接修改文件内容,而不是输出到终端,
如果不使用-i选项sed软件只是修改在内存中的数据,并不会影响磁盘上的文件
sed命令
command 解释
a(append) 追加,在指定行后面添加一行或多行文本
i(insert) 插入,指在指定行前添加一行或多行文本
c(chenge) 取代指定行
d(delete) 删除指定行
p(print ) 打印模式空间内容,通常与-n一起使用
! 对指定行以外所有行应用命令
sed正删改查

演示文件
cat test
this is the first line
this is the second line
this is the third line
this is the forth line
this is the fivth line
this is the sixth line
this is the seventh line
this is the eighth line
this is the ninth line
this is the tenth line

1.增

实例1
a :追加,在指定行后面添加一行或多行文本
sed “2a new” test
this is the first line
this is the second line
new
this is the third line
this is the forth line
this is the fivth line
this is the sixth line
this is the seventh line
this is the eighth line
this is the ninth line
this is the tenth line

实例2
i : 指在指定行前添加一行或多行文本
sed “2i new” test
this is the first line
new
this is the second line
this is the third line
this is the forth line
this is the fivth line
this is the sixth line
this is the seventh line
this is the eighth line
this is the ninth line
this is the tenth line

实例3
\n:同时添多行
sed “2a new\nnew\nnew” test
this is the first line
this is the second line
new
new
new
this is the third line
this is the forth line
this is the fivth line
this is the sixth line
this is the seventh line
this is the eighth line
this is the ninth line
this is the tenth line

2、删

实例1
d:删除所有
sed “d” test

实例2
d:删除指定行
sed “3d” test
this is the first line
this is the second line
this is the forth line
this is the fivth line
this is the sixth line
this is the seventh line
this is the eighth line
this is the ninth line
this is the tenth line

实例3
d:删除范围行
sed “2,4d” test
this is the first line
this is the fivth line
this is the sixth line
this is the seventh line
this is the eighth line
this is the ninth line
this is the tenth line

实例4
正则:删除匹配行
sed “/f.*/d” test
this is the second line
this is the third line
this is the sixth line
this is the seventh line
this is the eighth line
this is the ninth line
this is the tenth line

实例5
!:取反
sed ‘/f.*/!d’ test
this is the first line
this is the forth line
this is the fivth line

3、改

实例1
c(change):替换
sed ‘2c change’ test
this is the first line
change
this is the third line
this is the forth line
this is the fivth line
this is the sixth line
this is the seventh line
this is the eighth line
this is the ninth line
this is the tenth line

实例2
s///g :替换

s:替换
g:全局替换
-i:直接修改文件内容(谨慎使用建议先备份)

sed ‘s/is/ese/’ test
these is the first line
these is the second line
these is the third line
these is the forth line
these is the fivth line
these is the sixth line
these is the seventh line
these is the eighth line
these is the ninth line
these is the tenth line

sed ‘s/is/ese/g’ test
these ese the first line
these ese the second line
these ese the third line
these ese the forth line
these ese the fivth line
these ese the sixth line
these ese the seventh line
these ese the eighth line
these ese the ninth line
these ese the tenth line

#寻找(以d开头的行)并替换
[root@localhost ~]# sed -i ‘/^d/ {s/ss/dd/g}’ /root/test
[root@localhost ~]# a=dd
[root@localhost ~]# b=ss
[root@localhost ~]# sed -i ‘/^d/ {s/‘ a ′ / ′ a'/' a/b’/g}’ /root/test
[root@localhost ~]# echo ‘$a’
$a

实例3
-r 扩展正则:指定替换
sed -r ‘s/(first) line/\1 hang/g’ test
this is the first hang
this is the second line
this is the third line
this is the forth line
this is the fivth line
this is the sixth line
this is the seventh line
this is the eighth line
this is the ninth line
this is the tenth line

4、查

实例1
p (print):输出指定内容,但默认会输出2次匹配的结果,因此使用-n选项取消默认输出
/ / 正则:匹配
sed -r -n ‘/f.*/p’ test
this is the first line
this is the forth line
this is the fivth line
-e:多点操作

sed -n -e ‘5p’ -e ‘7p’ -e ‘1p’ test
this is the first line
this is the fivth line
this is the seventh line

grep

作用:过滤文本内容

选项 描述
-E :–extended–regexp 模式是扩展正则表达式(ERE)
-i :–ignore–case 忽略大小写
-n: --line–number 打印行号
-o:–only–matching 只打印匹配的内容
-c:–count 只打印每个文件匹配的行数
-B:–before–context=NUM 打印匹配的前几行
-A:–after–context=NUM 打印匹配的后几行
-C:–context=NUM 打印匹配的前后几行
–color[=WHEN] 匹配的字体颜色,别名已定义了
-v:–invert–match 打印不匹配的行
-e 多点操作eg:grep -e “^s” -e “s$”

样本文件内容
[root@ken ~]# cat test
dlakdlad
ad
ad
a
dFSAF
A
F
F
AS
F
f
sf
as
f

实例1:打印出所有的a无论大小写 : -i选项
grep -i “a” test
dlakdlad
ad
ad
a
dFSAF
A
AS
as

实例2:打印出所有的a无论大小写,并且显示该字符串所在的行 : -n选项
grep -i -n “a” test
1:dlakdlad
2:ad
3:ad
4:a
5:dFSAF
6:A
9:AS
13:as

实例3:仅仅打印出所有匹配的字符串: -o选项
grep -i -o “a” test
a
a
a
a
a
A
A
A
a

实例4:打印出匹配的字符串有多少行 -c选项
grep -i -c “a” test
8

实例5:打印出字符S前面的2行 -B
grep -B 2 “S” test
ad
a
dFSAF

F
F
AS

实例6:打印出字符S后面的2行 -A
grep -A 2 “S” test
dFSAF
A
F

AS
F
f

实例7:打印出字符S前后2行 -C
grep -C 2 “S” test
ad
a
dFSAF
A
F
F
AS
F
f

实例8:打印出不包含大小s的所有行 取反 -v
grep -i -v “s” test
dlakdlad
ad
ad
a
A
F
F
F
f
f

grep可以从文件当中直接搜索某个关键词,也可以从标准输入里面搜错
grep root /etc/passwd
root❌0:0:root:/root:/bin/bash
operator❌11:0:operator:/root:/sbin/nologin

cat /etc/passwd | grep “root”
root❌0:0:root:/root:/bin/bash
operator❌11:0:operator:/root:/sbin/nologin

正则表达式(基于grep)
功能就是用来检索、替换那些符合某个模式(规则)的文本,正则表达式在每种语言中都会有;
正则表达式就是为了处理大量的文本或字符串而定义的一套规则和方法
通过定义的这些特殊符号的辅助,系统管理员就可以快速过滤,替换或输出需要的字符串
Linux正则表达式一般以行为单位处理
基础正则表达式
符号 描述
. 匹配任意单个字符(必须存在)
^ 匹配以某个字符开头的行
$ 配以什么字符结尾的行

  • 匹配前面的一个字符出现0次或者多次;eg:ab
    .
    表示任意长度的任意字符
    [] 表示匹配括号内的一个字符
    [^] 匹配[^字符]之外的任意一个字符
    [] 匹配非[^字符]内字符开头的行
    < 锚定 单词首部;eg:<root
    > 锚定 单词尾部:eg:>root
    {m,n} 表示匹配前面的字符出现至少m次,至多n次
    () 表示对某个单词进行分组;\1表示第一个分组进行调用
    扩展正则
    egrep …
    grep -E …
    扩展正则支持所有基础正则;并有补充
    扩展正则中{}和[]不用转义可以直接使用;
    符号 描述
  • 表示前面的字符至少出现1次的情况
    | 表示“或”
    ? 表示前面的字符至多出现1次的情况
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值