文本处理三剑客简介(grep、awk、sed)
本章内容:
| 命令 | 描述 |
|---|---|
| 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处理过程:
- 读取文本第一行,首先会和匹配模式进行相匹配,如果发现第一行内容不和模式相匹配的话,就继续读取下一行
- 读取到第二行发现和模式相匹配,就会执行花括号里面的动作
- 然后继续读取下一行,发现和模式相匹配,就会执行花括号里面的动作
…- 依次读取全文
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工作原理
- 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正删改查
演示文件
╭─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)的更多相关文章
- Linux文本处理三剑客之grep及正则表达式详解
Linux文本处理三剑客之grep及正则表达式详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Linux文本处理三剑客概述 grep: 全称:"Global se ...
- linux文本处理三剑客之 grep
文本处理无非是对文本内容做查看.修改等操作.Linux三剑客: grep.sed 和 awk 命令. 处理文本内容,用 Vim 编辑器不是很好吗?Vim 允许我们使用键盘.鼠标来对文本内容进行交互性地 ...
- Linux文本处理三剑客之——grep
一Linux文本处理三剑客之——grep Linux文本处理三剑客都支持正则表达式 grep :文本过滤( 模式:pattern) 工具,包括grep, egrep, fgrep (不支持正则表达式) ...
- Linux的文本处理工具浅谈-awk sed grep
Linux的文本处理工具浅谈 awk 老大 [功能说明] 用于文本处理的语言(取行,过滤),支持正则 NR代表行数,$n取某一列,$NF最后一列 NR==20,NR==30 从20行到30行 FS ...
- Linux文件处理三剑客(grep、sed、awk)
下面所说的是Linux中最重要的三个命令在业界被称为"三剑客",它们是grep.sed.awk. 我们现在知道Linux下一切皆文件,对Linux的操作就是对文件的处理,那么怎么能 ...
- shell脚本三剑客:grep、sed、awk
shell脚本三剑客:grep sed awk grep语法: grep [OPTIONS] PATTERN [FILE] 常用选项 -c 统计匹配到的 ...
- Linux三剑客-grep || awk || sed
grep是一个强大的文本搜索工具 命令格式: grep [option] pattren file -a 将二进制文档以文本方式处理 -c 计算找到的符合行的次数 -i 忽略大小写 -n 顺便 ...
- Linux文本处理三剑客之grep
简介 grep命令,用于在一个文本文件中或者从STDIN中,根据用户给出的模式(pattern)过滤出所需要的信息. grep以及三剑客中的另外两个工具sed和awk都是基于行处理的,它们会一行行读入 ...
- 文本处理三剑客之 grep
grep简介 grep(Global search REgular expression and Print out the line)是Linux上的文本处理三剑客之一,另外两个是sed和awk. ...
- Linux 文本处理三剑客之grep
文本处理都要使用正则表达式,正则表达式有: 基本正则表达式:grep或者egrep -G 扩展正则表达式:egreo或者grep -E Linux 文本处理三剑客: sed:stream editor ...
随机推荐
- eclipse下的jetty远程调试设置
在windows下的jetty远程调试设置1.首先说明的是windows下的jetty是通过命令java -jar start.jar来启动的,因此要想设置成远程调试模式,则要改用命令java -X ...
- 使用纯 CSS 实现滚动阴影效果
开门见山,有这样一种非常常见的情况,对于一些可滚动的元素而言.通常在滚动的时候会给垂直于滚动的一侧添加一个阴影,用于表明当前有元素被滚动给该滚出了可视区域,类似这样: 可以看到,在滚动的过程中,会出现 ...
- 利用DES,C#加密,Java解密代码
//C#加密 /// <summary> /// 进行DES加密. /// </summary> /// <param name="pToEncrypt&quo ...
- 【SpringMVC】SpringMVC 响应数据
SpringMVC 响应数据 文章源码 返回值分类 返回值是字符串 Controller 方法返回字符串可以指定逻辑视图的名称,通过视图解析器解析为物理视图的地址. @Controller @Requ ...
- 剑指offer 面试题10.2:青蛙变态跳台阶
题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级--它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 编程思想 因为n级台阶,第一步有n种跳法:跳1级.跳2级.到跳n级跳1级,剩下 ...
- Docker 镜像仓库使用(六)
阿里云docker 容器镜像服务: www.aliyun.com 1 服务开通 (开通的时候要求创建密码请牢记此密码) 2 创建命名空间 3 创建镜像仓库 4 linux 客户端登录 登录: dock ...
- 【win10】win10下两个显示器不同桌面壁纸
win10系统下,双屏显示为不同的桌面壁纸 操作: 1.鼠标右键点击个性化 2.点击背景选项 3.在图片上右键选择要添加为背景的图片 同理,将另一个屏幕壁纸设为监视器1 最后效果为两个分屏为不同桌面壁 ...
- logicaldisk本地磁盘管理
在网上搜了很多,但是基本都是一样的,差不多都是互相转载摘抄,就那么几个寥寥无几的例子,所以我冒了很大的风险,自己经过多次的测试,对这个命令有了一些新的认识!拿出来分享一下! LOGICALDISK ...
- Spring-AOP为类增加新的功能
适配器的简单应用实现: 比如:有一个类Car,在类中有两个属性一个为汽车名name,另一个为速度speed.其行为为run(). 现在有一辆车BMWCar 增加了GPS功能.如下实现: 基本类: pu ...
- mysql+MHA高可用
MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton(现就职于Facebook公司)开发,是一套优秀 ...