本章内容:

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

awk

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

推荐文章:http://www.zsythink.net/?s=awk

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

1、awk语法

awk [option] 'pattern {action}' filename

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

option:

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

实例:

╭─root@localhost.localdomain ~
╰─➤ 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开头的行

╭─root@localhost.localdomain ~
╰─➤ awk '/^root/{print $0}' test
root:x:0:0:root:/root:/bin/bash
╭─root@localhost.localdomain ~
╰─➤ awk '/^root/'test
root:x:0:0:root:/root:/bin/bash

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

# 第五个字段包含root  打印出来( “~ ”  正则匹配)

╭─root@localhost.localdomain ~
╰─➤ awk -F ":" '$5~/root/{print $0}' test
root:x:0:0:root:/root:/bin/bash

实例3:打印本机ip地址

╭─root@localhost.localdomain ~
╰─➤ ip a|grep global|awk -F " +|/" '{print $3}'
192.168.137.6 # 或 ╭─root@localhost.localdomain ~
╰─➤ 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:

╭─root@localhost.localdomain ~
╰─➤ 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
╭─root@localhost.localdomain ~
╰─➤ awk 'BEGIN{i=0}/root/{i++}END{print i}' test
2
╭─root@localhost.localdomain ~
╰─➤ awk '/root/{i++}END{print i}' test
2

实例2:统计访问的网址

#用cut+sort+uniq的方法

╭─root@localhost.localdomain ~
╰─➤ 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的方法 ╭─root@localhost.localdomain ~
╰─➤ 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正删改查

演示文件

╭─root@localhost.localdomain ~
╰─➤ 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 :追加,在指定行后面添加一行或多行文本

╭─root@localhost.localdomain ~
╰─➤ 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 : 指在指定行前添加一行或多行文本

╭─root@localhost.localdomain ~
╰─➤ 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:同时添多行

╭─root@localhost.localdomain ~
╰─➤ 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:删除指定行

╭─root@localhost.localdomain ~
╰─➤ 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:删除范围行

╭─root@localhost.localdomain ~
╰─➤ 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

正则:删除匹配行

╭─root@localhost.localdomain ~
╰─➤ 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

!:取反

╭─root@localhost.localdomain ~
╰─➤ sed '/f.*/!d' test
this is the first line
this is the forth line
this is the fivth line

3、改

实例1

c(change):替换

╭─root@localhost.localdomain ~
╰─➤ 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:直接修改文件内容(谨慎使用建议先备份)

╭─root@localhost.localdomain ~
╰─➤ 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
╭─root@localhost.localdomain ~
╰─➤ 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'/'$b'/g}' /root/test
[root@localhost ~]# echo '$a'
$a

实例3

-r 扩展正则:指定替换

╭─root@localhost.localdomain ~
╰─➤ 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选项取消默认输出

/ / 正则:匹配

╭─root@localhost.localdomain ~
╰─➤ sed -r -n '/f.*/p' test
this is the first line
this is the forth line
this is the fivth line

-e:多点操作

╭─root@localhost.localdomain ~
╰─➤ 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选项

╭─root@localhost.localdomain ~
╰─➤ grep -i -n “a” test
1:dlakdlad
2:ad
3:ad
4:a
5:dFSAF
6:A
9:AS
13:as

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

╭─root@localhost.localdomain ~
╰─➤ grep -i -o “a” test
a
a
a
a
a
A
A
A
a

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

╭─root@localhost.localdomain ~
╰─➤ grep -i -c “a” test
8

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

╭─root@localhost.localdomain ~
╰─➤ grep -B 2 “S” test
ad
a
dFSAF

F
F
AS

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

╭─root@localhost.localdomain ~
╰─➤ grep -A 2 “S” test
dFSAF
A
F

AS
F
f

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

╭─root@localhost.localdomain ~
╰─➤ grep -C 2 “S” test
ad
a
dFSAF
A
F
F
AS
F
f

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

╭─root@localhost.localdomain ~
╰─➤ 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次的情况

文本处理三剑客简介(grep、awk、sed)的更多相关文章

  1. Linux文本处理三剑客之grep及正则表达式详解

    Linux文本处理三剑客之grep及正则表达式详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Linux文本处理三剑客概述 grep: 全称:"Global se ...

  2. linux文本处理三剑客之 grep

    文本处理无非是对文本内容做查看.修改等操作.Linux三剑客: grep.sed 和 awk 命令. 处理文本内容,用 Vim 编辑器不是很好吗?Vim 允许我们使用键盘.鼠标来对文本内容进行交互性地 ...

  3. Linux文本处理三剑客之——grep

    一Linux文本处理三剑客之——grep Linux文本处理三剑客都支持正则表达式 grep :文本过滤( 模式:pattern) 工具,包括grep, egrep, fgrep (不支持正则表达式) ...

  4. Linux的文本处理工具浅谈-awk sed grep

    Linux的文本处理工具浅谈 awk   老大 [功能说明] 用于文本处理的语言(取行,过滤),支持正则 NR代表行数,$n取某一列,$NF最后一列 NR==20,NR==30 从20行到30行 FS ...

  5. Linux文件处理三剑客(grep、sed、awk)

    下面所说的是Linux中最重要的三个命令在业界被称为"三剑客",它们是grep.sed.awk. 我们现在知道Linux下一切皆文件,对Linux的操作就是对文件的处理,那么怎么能 ...

  6. shell脚本三剑客:grep、sed、awk

    shell脚本三剑客:grep  sed  awk grep语法: grep [OPTIONS] PATTERN [FILE] 常用选项 -c                       统计匹配到的 ...

  7. Linux三剑客-grep || awk || sed

    grep是一个强大的文本搜索工具 命令格式: grep [option] pattren file -a  将二进制文档以文本方式处理 -c  计算找到的符合行的次数 -i  忽略大小写 -n  顺便 ...

  8. Linux文本处理三剑客之grep

    简介 grep命令,用于在一个文本文件中或者从STDIN中,根据用户给出的模式(pattern)过滤出所需要的信息. grep以及三剑客中的另外两个工具sed和awk都是基于行处理的,它们会一行行读入 ...

  9. 文本处理三剑客之 grep

    grep简介 grep(Global search REgular expression and Print out the line)是Linux上的文本处理三剑客之一,另外两个是sed和awk. ...

  10. Linux 文本处理三剑客之grep

    文本处理都要使用正则表达式,正则表达式有: 基本正则表达式:grep或者egrep -G 扩展正则表达式:egreo或者grep -E Linux 文本处理三剑客: sed:stream editor ...

随机推荐

  1. Kubernetes官方java客户端之五:proto基本操作

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  2. 性能超四倍的高性能.NET二进制序列化库

    二进制序列化在.NET中有很多使用场景,如我们使用分布式缓存时,通常将缓存对象序列化为二进制数据进行缓存,在ASP.NET中,很多中间件(如认证等)也都是用了二进制序列化. 在.NET中我们通常使用S ...

  3. uni-app 页面跳转的两种方法

    1.navigator  标签 <navigator url="../component/classdetails/classdetails"> <view cl ...

  4. #2020征文-开发板# 用鸿蒙开发AI应用(三)软件篇

    目录: 前言 HarmonyOS 简介 DevEco Device Tool(windows下) 获取源码(切换到ubuntu) 烧录程序(切换回windows) 前言上一篇,我们在 Win10 上用 ...

  5. .NET 云原生架构师训练营(模块二 基础巩固 RabbitMQ Masstransit 异常处理)--学习笔记

    2.6.8 RabbitMQ -- Masstransit 异常处理 异常处理 其他 高级功能 异常处理 异常与重试 重试配置 重试条件 重新投递信息 信箱 异常与重试 Exception publi ...

  6. 【C++】《Effective C++》第九章

    杂项讨论 条款53:不要轻忽编译器的警告 请记住 严肃对待编译器发出的警告信息.努力在你的编译器的最高(最严苛)警告级别下争取"无任何警告"的容易. 不要过度依赖编译器的报警能力, ...

  7. Python3爬取小说并保存到文件

    问题 python课上,老师给同学们布置了一个问题,因为这节课上学的是正则表达式,所以要求利用python爬取小说网的任意小说并保存到文件. 我选的网站的URL是'https://www.biquka ...

  8. Java 多线程读取文件并统计词频 实例 出神入化的《ThreadPoolExecutor》

    重在展示多线程ThreadPoolExecutor的使用,和线程同步器CountDownLatch,以及相关CAS的原子操作和线程安全的Map/队列. ThreadPool主线程 1 import j ...

  9. 【Vue】Vue开发环境搭建

    Vue前置学习环境 文章目录 Vue前置学习环境 IDE Node.js 调试环境 工程环境 小结 IDE WebStorm 官网下载:https://www.jetbrains.com/websto ...

  10. SDUST数据结构 - chap1 绪论

    一.判断题: 二.选择题: