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

本章内容:

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

awk

推荐文章:https://www.cnblogs.com/ginvip/p/6352157.html

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

1、awk语法

awk [option] 'pattern {action}' filename

命令 + 选项 +   找谁 +   干啥  +    文件名 

option:
-F :指定分隔符(不指定默认以空格为分隔符)

实例:

╭─[email protected] ~  
╰─➤  awk ‘NR>=2&&NR<=5{print $0}’ test
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
i#adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

分析awk处理过程:

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

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 …$n 当前记录的第n个字段,字段间由FS分隔
$NF 输出最后一个字段
$0 输出整条记录

3、awk运算符

运算符 描述
= += -= *= /= %= ^= **= 赋值
|| 逻辑或
&& 逻辑与
~ ~! 匹配正则表达式和不匹配正则表达式
< <= > >= != == 关系运算符
+ - 加,减
* / & 乘,除与求余
+ - ! 一元加,减和逻辑非
^ *** 求幂
++ -- 增加或减少,作为前缀或后缀

4、awk-正则

基础正则:

符号 描述
. 匹配任意单个字符(必须存在)
^ 匹配以某个字符开头的行
$ 配以什么字符结尾的行
* 匹配前面的一个字符出现0次或者多次;eg:a*b
.* 表示任意长度的任意字符
[] 表示匹配括号内的一个字符
[^] 匹配[^字符]之外的任意一个字符
^[^] 匹配非[^字符]内字符开头的行
\< 锚定 单词首部;eg:\<root
\> 锚定 单词尾部:eg:\>root

扩展正则:

符号 描述
+ 表示前面的字符至少出现1次的情况
| 表示“或”
表示前面的字符至多出现1次的情况

实例1:打印出来以root开头的行

╭─[email protected] ~  
╰─➤  awk '/^root/{print $0}' test
root:x:0:0:root:/root:/bin/bash
╭─[email protected] ~  
╰─➤ awk '/^root/'test
root:x:0:0:root:/root:/bin/bash

实例2:第五个字段包含root的行

╭─[email protected] ~  
╰─➤ awk -F ":"  '$5~/root/{print $0}'  test
root:x:0:0:root:/root:/bin/bash

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

语法(awk基本结构):

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

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

awk数组

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

实例1:

╭─[email protected] ~  
╰─➤   cat test
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
i#adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
╭─[email protected] ~  
╰─➤ awk 'BEGIN{i=0}/root/{i++}END{print i}' test
2
╭─[email protected] ~  
╰─➤ awk '/root/{i++}END{print i}' test
2

实例2:统计访问的网址

#用cut+sort+uniq的方法

╭─[email protected] ~  
╰─➤   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的方法

╭─[email protected] ~  
╰─➤  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工作原理

  1. sed读取一行,首先将这行放入到缓存中
  2. 然后,才对这行进行处理
  3. 处理完成以后,将缓冲区的内容发送到终端
  • 存储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正删改查

演示文件

╭─[email protected] ~  
╰─➤  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 :追加,在指定行后面添加一行或多行文本

╭─[email protected] ~  
╰─➤  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 : 指在指定行前添加一行或多行文本

╭─[email protected] ~  
╰─➤  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:同时添多行

╭─[email protected] ~  
╰─➤  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:删除所有

╭─root@localhost.localdomain ~  
╰─➤  sed "d" test

实例2
d:删除指定行

╭─[email protected] ~  
╰─➤  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:删除范围行

╭─[email protected] ~  
╰─➤  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
正则:删除匹配行

╭─[email protected] ~  
╰─➤  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
!:取反

╭─[email protected] ~  
╰─➤  sed '/f.*/!d' test
this is the first line
this is the forth line
this is the fivth line

3、改

实例1
c(change):替换

╭─[email protected] ~  
╰─➤  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:直接修改文件内容(谨慎使用建议先备份)

╭─[email protected] ~  
╰─➤  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
╭─[email protected] ~  
╰─➤  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

实例3
-r 扩展正则:指定替换

╭─[email protected] ~  
╰─➤  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选项取消默认输出
/ / 正则:匹配

╭─[email protected] ~  
╰─➤  sed -r -n '/f.*/p' test
this is the first line
this is the forth line
this is the fivth line

-e:多点操作

╭─[email protected] ~  
╰─➤  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选项

╭─root@localhost.localdomain ~  
╰─➤   grep -i “a” test
dlakdlad
ad
ad
a
dFSAF
A
AS
as

实例2:打印出所有的a无论大小写,并且显示该字符串所在的行 : -n选项

╭─[email protected] ~  
╰─➤   grep -i -n “a” test
1:dlakdlad
2:ad
3:ad
4:a
5:dFSAF
6:A
9:AS
13:as

实例3:仅仅打印出所有匹配的字符串: -o选项

╭─[email protected] ~  
╰─➤   grep -i -o “a” test
a
a
a
a
a
A
A
A
a

实例4:打印出匹配的字符串有多少行 -c选项

╭─[email protected] ~  
╰─➤   grep -i -c “a” test
8

实例5:打印出字符S前面的2行 -B

╭─[email protected] ~  
╰─➤  grep -B 2 “S” test
ad
a
dFSAF
—
F
F
AS

实例6:打印出字符S后面的2行 -A

╭─[email protected] ~  
╰─➤  grep -A 2 “S” test
dFSAF
A
F
—
AS
F
f

实例7:打印出字符S前后2行 -C

╭─[email protected] ~  
╰─➤   grep -C 2 “S” test
ad
a
dFSAF
A
F
F
AS
F
f

实例8:打印出不包含大小s的所有行 取反 -v

╭─[email protected] ~  
╰─➤   grep -i -v “s” test
dlakdlad
ad
ad
a
A
F
F
F
f
f

grep可以从文件当中直接搜索某个关键词,也可以从标准输入里面搜错

╭─root@localhost.localdomain ~  
╰─➤   grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
╭─root@localhost.localdomain ~  
╰─➤   cat /etc/passwd | grep “root”
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

正则表达式(基于grep)

  • 功能就是用来检索、替换那些符合某个模式(规则)的文本,正则表达式在每种语言中都会有;
  • 正则表达式就是为了处理大量的文本或字符串而定义的一套规则和方法
  • 通过定义的这些特殊符号的辅助,系统管理员就可以快速过滤,替换或输出需要的字符串
  • Linux正则表达式一般以行为单位处理

基础正则表达式

符号 描述
. 匹配任意单个字符(必须存在)
^ 匹配以某个字符开头的行
$ 配以什么字符结尾的行
* 匹配前面的一个字符出现0次或者多次;eg:a*b
.* 表示任意长度的任意字符
[] 表示匹配括号内的一个字符
[^] 匹配[^字符]之外的任意一个字符
^[^] 匹配非[^字符]内字符开头的行
\< 锚定 单词首部;eg:\<root
\> 锚定 单词尾部:eg:\>root
\{m,n\} 表示匹配前面的字符出现至少m次,至多n次
\(\) 表示对某个单词进行分组;\1表示第一个分组进行调用

扩展正则

  • egrep ...
  • grep -E ...
  • 扩展正则支持所有基础正则;并有补充
  • 扩展正则中{}和[]不用转义可以直接使用;
符号 描述
+ 表示前面的字符至少出现1次的情况
| 表示“或”
表示前面的字符至多出现1次的情况

原文链接:https://www.cnblogs.com/du-z/p/10973641.html

猜你喜欢

转载自blog.csdn.net/u014532775/article/details/94360236