Shell 正则表达式基础详解(二)

前言:

上篇博客我们介绍了正则表达式中grep与egrep的元字符功能,此篇博客将介绍文本编辑器

一、文本处理器

在Linux/UNIX 系统中包含很多文本处理器或文本编辑器,其中包含VIM编辑器与grep等。而grep,sed,awk更是Shell编程中经常用到的文本处理工具,被称为Shell编程三剑客。

二、sed工具

2.1 sed 工具介绍

sed是一个强大而简单的文本解析转换工具,可以读取文本,并根据指定的条件对文本内容进行编辑(删除、替换、添加、移动等),最后输出所有行或者仅输出处理的某些行。sed也可以在五交互的情况下实现相当复杂的文本处理操作,被广泛应用于Shell脚本中,用以完成各种自动化处理任务。

2.2 sed工作流程及常见用法
2.2.1 工作流程

读取:sed从输入流(文件、管道、标准输入)中读取以行内容并存储到临时的缓冲区中(又称模式空间)

执行:默认情况下,所有sed命令都在模式空间中顺序地执行,除非指定了行的地址,否则sed命令将会在所有的行依次执行。

显示:发送修改后的内容到输出流。再发送数据后,模式空间将会被请空。在所有的文件内容都被处理完成之前,上述过程将重复执行,直至所有内容被处理完。

需注意:默认情况下,所有的sed命令都是在模式空间内执行的,因此输入的文件并不会发生任何变化,除非是用重定向存储输出。

2.2.2 sed命令常见用法

通常情况下调用sed命令有两种格式,其中,“参数”是指操作的目标文件,当存在多个操作对象时使用,文件之间用逗号“,”分割;而scriptfile表示脚本文件,需要用“-f”选项指定,当脚本文件出现在目标文件之前时,表示通过指定的脚本文件来处理输入的目标文件。

格式1:sed [选项] ‘操作’ 参数

格式2:sed [选项] -f scriptfile 参数

常见的sed命令选项:

在这里插入图片描述
“操作” 用于指定对文本操作的动作行为,也就是sed的命令。通常情况下是采用的“[n1[n2]]”操作参数的格式。n1、n2是可选的,不一定会存在,代表选择进行操作的行数,如操作需要在5~20行之间进行,则表示为“5,20 动作行为”。

常见的操做:
在这里插入图片描述

2.3 用法示例

本篇博客依然以test.txt 文件进行演示:

[root@localhost opt]# cat test.txt
1、he was short and fat.
2、He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
4、The year ahead will test our political establishment to the limit.
5、PI=3.141592653589793238462643383249901429
6、a wood cross!
7、Actions speak louder than words
8、the test is a kidding
9、the man is true
10、#woood # #woooooood # AxyzxyzxyzxyzC
11、I bet this place is really spooky late at night! Misfortunes never come alone/single.
12、wd
13、wod
14、I shouldn't have lett so tast.

示例1:输出符合条件的文本(p 表示正常输出)

[root@localhost opt]# sed -n 'p' test.txt 
1、he was short and fat.
2、He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
4、The year ahead will test our political establishment to the limit.
5、PI=3.141592653589793238462643383249901429
6、a wood cross!
7、Actions speak louder than words
8、the test is a kidding
9、the man is true
10、#woood # #woooooood # AxyzxyzxyzxyzC
11、I bet this place is really spooky late at night! Misfortunes never come alone/single.
12、wd
13、wod
14、I shouldn't have lett so tast.

示例2:输出第三行

[root@localhost opt]# sed -n '3p' test.txt 
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.

示例3:输出3~5行

[root@localhost opt]# sed -n '3,5p' test.txt 
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
4、The year ahead will test our political establishment to the limit.
5、PI=3.141592653589793238462643383249901429

示例4:输出所有奇数行,n表示读入下一行资料

[root@localhost opt]# sed -n 'p;n' test.txt 
1、he was short and fat.
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
5、PI=3.141592653589793238462643383249901429
7、Actions speak louder than words
9、the man is true
11、I bet this place is really spooky late at night! Misfortunes never come alone/single.
13、wod

示例5:输出所有偶数行,n表示读入下一行资料

[root@localhost opt]# sed -n 'n;p' test.txt 
2、He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
4、The year ahead will test our political establishment to the limit.
6、a wood cross!
8、the test is a kidding
10、#woood # #woooooood # AxyzxyzxyzxyzC
12、wd
14、I shouldn't have lett so tast.

示例6:输出第1~5行之间的奇数行(第1、3、5行)

[root@localhost opt]# sed -n '1,5{p;n}' test.txt 
1、he was short and fat.
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
5、PI=3.141592653589793238462643383249901429

需注意,如果输出第2~6行之间的奇数行,如下:

[root@localhost opt]# sed -n '2,6{n;p}' test.txt 
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
5、PI=3.141592653589793238462643383249901429
7、Actions speak louder than words

首先在输入时 n需要与p位置互换
在输出结果后,我们会发现输出了第7行,这是因为”{n;p}“在执行到第6行时,会执行”n“读入下一行资料,然后输出”p“,即第7行的内容。

示例7:输出第10行至文件尾之间的偶数行

[root@localhost opt]# sed -n '10,${p;n}' test.txt 
10、#woood # #woooooood # AxyzxyzxyzxyzC
12、wd
14、I shouldn't have lett so tast.

在执行”sed -n ‘10,${p:n}’ test.txt “命令时,读取的第一行时文件的第10行,直接打印,下一行行是第11行,读取下一行后输出第12行,以此类推,所以输出的偶数行是文件的第10行、12行直至结尾,如有空行在偶数行也会进行输出。

以上是sed命令的基本用法。

sed命令结合正则表达式,格式略有不同,正则表达式以”/“包围。例如,以下操作是sed命令与正则表达式结合使用的示例。

示例7:输出包含the的行

[root@localhost opt]# sed -n '/the/p' test.txt 
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
4、The year ahead will test our political establishment to the limit.

示例8:输出从第4行至第一个包含the的行

[root@localhost opt]# sed -n '4,/the/p' test.txt 
4、The year ahead will test our political establishment to the limit.
5、PI=3.141592653589793238462643383249901429
6、a wood cross!
7、Actions speak louder than words
8、the test is a kidding

示例9:输出包含the的行所在的行号,等号(=)用来输出行号

[root@localhost opt]# sed -n '/the/=' test.txt 
3
4
8
9

示例10:输出以5、PI开头的行

[root@localhost opt]# sed -n '/^5、PI/p' test.txt 
5、PI=3.141592653589793238462643383249901429

示例11:输出以数字结尾的行

[root@localhost opt]# sed -n '/[0-9]$/p' test.txt 
5、PI=3.141592653589793238462643383249901429

示例12:输出包含单词wood的行:

[root@localhost opt]# sed -n '/\<wood\>/p' test.txt 
6、a wood cross!
2.4 删除符合条件的文本(d)

因为之后的示例还需要使用测试文件 test.txt,所以在执行删除操作之前需要我们先将测试文件备份。

以下为test.txt的副本:

[root@localhost opt]# cat test1.txt
1、he was short and fat.
2、He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
4、The year ahead will test our political establishment to the limit.
5、PI=3.141592653589793238462643383249901429
6、a wood cross!
7、Actions speak louder than words

8、the test is kidding

9、the man is true
10、#woood # #woooooood # AxyzxyzxyzxyzC
11、I bet this place is really spooky late at night! Misfortunes never come alone/single.
12、wd

13、wod
14、I shouldn't have lett so tast.

下面命令中nl命令用于计算文件的行数,结合该命令可以更直观地查看到命令执行的结果。

示例1:删除第三行

[root@localhost opt]# nl test1.txt | sed '3d'
     1  1、he was short and fat.
     2  2、He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
     4  4、The year ahead will test our political establishment to the limit.
     5  5、PI=3.141592653589793238462643383249901429
     6  6、a wood cross!
     7  7、Actions speak louder than words
.......省略以下内容

示例2:删除3~5行

[root@localhost opt]# nl test1.txt | sed '3,5d'
     1  1、he was short and fat.
     2  2、He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
     6  6、a wood cross!
     7  7、Actions speak louder than words
......省略以下内容

示例3:删除包含cross的行,原本的第6行被删除
删除不包含cross的行,用!符号表示取反操作,如‘/cross/!d’

[root@localhost opt]# nl test1.txt | sed '/cross/d'
     1  1、he was short and fat.
     2  2、He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
     3  3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
     4  4、The year ahead will test our political establishment to the limit.
     5  5、PI=3.141592653589793238462643383249901429
     7  7、Actions speak louder than words
       
     8  8、the test is kidding
       
     9  9、the man is true
    10  10、#woood # #woooooood # AxyzxyzxyzxyzC
    11  11、I bet this place is really spooky late at night! Misfortunes never come alone/single.
    12  12、wd
       
    13  13、wod
    14  14、I shouldn't have lett so tast.

以上可见,原第6行包含cross的行已被删除

示例4:删除以小写字母开头的行 (我们在test1.txt中的首行添加2行以小写字母开头的字符:

[root@localhost opt]# cat test1.txt 
abcde
fghi
1、he was short and fat.
2、He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
......省略部分内容

删除以小写字母开头的行:

[root@localhost opt]# sed '/^[a-z]/d' test1.txt 
1、he was short and fat.
2、He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
.......省略部分内容

示例4:删除以”.“结尾的行

[root@localhost opt]# sed '/\.$/d' test1.txt 
abcde
fghi
5、PI=3.141592653589793238462643383249901429
6、a wood cross!
7、Actions speak louder than words

8、the test is kidding

9、the man is true
10、#woood # #woooooood # AxyzxyzxyzxyzC
12、wd

13、wod

示例5:删除所有空行

[root@localhost opt]# sed '/^$/d' test1.txt 
abcde
fghi
1、he was short and fat.
2、He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
4、The year ahead will test our political establishment to the limit.
5、PI=3.141592653589793238462643383249901429
6、a wood cross!
7、Actions speak louder than words
8、the test is kidding
9、the man is true
10、#woood # #woooooood # AxyzxyzxyzxyzC
11、I bet this place is really spooky late at night! Misfortunes never come alone/single.
12、wd
13、wod
14、I shouldn't have lett so tast.

需注意:若是删除重复的空行,即连续的空行只保留一个,执行”sed -e ‘/^KaTeX parse error: Expected group after '^' at position 6: /{n;/^̲/d}“ test.txt"命令即可实现。其效果与”cat -s test.txt"相同,n表示读取下一行数据。

2.5 替换符合条件的文本

在使用sed命令进行替换操作时需要用到s(字符串替换)、c(整行/整块替换)、y(字符转换)命令选项。

常见选项:
在这里插入图片描述

2.6 迁移符合条件的文本

其中,H,复制到剪贴板;g、G,将剪贴板中的数据覆盖/追加至指定行;w,保存为文件;r,读取指定文件;a,追加指定内容。

常用方法:

在这里插入图片描述

示例1:将包含the的行迁移至文件末尾,{;}用于多个操作

[root@localhost opt]# sed '/the/{H;d};$G' test.txt
1、he was short and fat.
2、He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
5、PI=3.141592653589793238462643383249901429
6、a wood cross!
7、Actions speak louder than words

10、#woood # #woooooood # AxyzxyzxyzxyzC
11、I bet this place is really spooky late at night! Misfortunes never come alone/single.
12、wd

13、wod
14、I shouldn't have lett so tast.

3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
4、The year ahead will test our political establishment to the limit.
8、the test is kidding
9、the man is true

示例2:将第1~5行内容转移至第7行后

[root@localhost opt]# sed '1,5{H;d};7G' test.txt 
6、a wood cross!
7、Actions speak louder than words

1、he was short and fat.
2、He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
4、The year ahead will test our political establishment to the limit.
5、PI=3.141592653589793238462643383249901429

8、the test is kidding
.......省略部分内容

示例3:将包含the的行另存为文件 out.file

[root@localhost opt]# sed '/the/w out.file' test.txt
......省略test.txt显示的内容
[root@localhost opt]# cat out.file
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
4、The year ahead will test our political establishment to the limit.
8、the test is kidding
9、the man is true

示例4:将文件/etc/hostname的内容添加到 test.txt中的包含the的行的下一行

[root@localhost opt]# cat /etc/hostname
localhost.localdomain

示例5:在第3行后插入一个新行,内容为New

[root@localhost opt]# sed '3aNew' test.txt
1、he was short and fat.
2、He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
New
4、The year ahead will test our political establishment to the limit.
........省略部分内容

示例6:在包含the的每行后插入一个新行,内容为New
在这里插入图片描述

示例7:在第3行后插入多行内容,中间的\n表示换行

[root@localhost opt]# sed '3aNew1\nNew2' test.txt
1、he was short and fat.
2、He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
New1
New2
........省略部分内容
2.7 使用脚本编辑文件

使用sed脚本,将多个编辑指令存放到文件中(每行一条编辑指令),通过“-f”选项来调用

示例1:将第1~5行内容转移至第7行后

我们在命令行可以用以下命令进行输出:

[root@localhost opt]# sed '1,5{H;d};7G' test.txt
6、a wood cross!
7、Actions speak louder than words

1、he was short and fat.
2、He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
4、The year ahead will test our political establishment to the limit.
5、PI=3.141592653589793238462643383249901429
.......省略部分内容

我们也可以使用脚本文件进行相同操作:
首先创建一个脚本,分行输入操作

[root@localhost opt]# cat opt.list 
1,5H   (剪切)
1,5d    (删除)
7G      (粘贴)

然后使用sed调用这个脚本:

[root@localhost opt]# sed -f opt.list test.txt 
6、a wood cross!
7、Actions speak louder than words

1、he was short and fat.
2、He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
4、The year ahead will test our political establishment to the limit.
5、PI=3.141592653589793238462643383249901429

8、the test is kidding
三、awk工具

在Linux/UNIX系统中,awk也是一个功能强大的编辑工具,逐行读取输入文本,并根据指定的匹配模式进行查找,对符合条件的内容进行格式化输出或者过滤处理,可以在无交互的情况下实现相当复杂的文本操作,被广泛应用于Shell脚本,完成各种自动化配置任务。

3.1 awk常见用法

通常情况下awk所使用的命令格式如下:

格式1:awk 选项 ’模式或条件(编辑指令)‘ 文件1 文件2
过滤并输出文件符合条件的内容

格式2:awk -f 脚本文件 文件1 文件2
从脚本中调用编辑指令,过滤并输出内容

sed命令常用于一整行的处理,而awk比较倾向于将一行分成多个“字段”然后再进行处理,且默认情况下字段的分隔符为空格或者tab键。awk执行结果可以通过print的功能将字段数据打印显示。

在使用awk命令的过程中,可以使用逻辑操作符:"&&“表示”与“,”||"表示“或”,“!”表示“非”;还可以进行简单的数学运算,如+、-、*、\、%、^分别表示加、减、乘、除、取余和乘方。

因为/etc/passwd/是一个非常典型的格式化文件,各字段使用“:”作为分隔符隔开,Linux系统中大部分日志文件也是格式化文件,从这些文件中提取相关信息是运维的日常工作内容之一。若需要查找除/etc/passwd的用户名、用ID、组ID等列,可通过执行以下awk命令。

示例1:输出/etc/passwd文件的第1、第3、第4列的内容

[root@localhost opt]# awk -F: '{print $1,$3,$4}' /etc/passwd
root 0 0
bin 1 1
daemon 2 2
adm 3 4
lp 4 7
sync 5 0
shutdown 6 0
halt 7 0
mail 8 12
operator 11 0
games 12 100
ftp 14 50
......省略部分内容

这里的$1、$3、$4 表示的是第几列而非位置变量。

awk从输入文件或者标准输入中读入信息,与sed 一样,信息的读入也是逐行读取。不同的是awk 将文件中的一行视为一个记录,而将一行中的某一部分(列)作为记录中的一个字段(域)。为了操作这些不同的字段,awk使用Shell中类似位置变量的方法,用$1 $2 $3 顺序地表示行(记录)中的不同字段。另外awk 用$0 表示整个行(记录)。不同的字段之间是通过指定的字符分割。awk默认的分割符是空格。awk允许在命令行中用“-F 分隔符“的形式来指定分隔符。因此,上述示例中,awk 命令对/etc/passwd 文件的工作原理如下图所示:

在这里插入图片描述

awk包含几个特殊的内建变量(可直接使用,不需要加”“)如下表:
在这里插入图片描述

3.2 用法示例
3.2.1 按行输出文本

示例1:输出所有内容,等同于cat test.txt~

方法一

[root@localhost opt]# awk '{print $0}' test.txt
1、he was short and fat.
2、He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
4、The year ahead will test our political establishment to the limit.
5、PI=3.141592653589793238462643383249901429
6、a wood cross!
7、Actions speak louder than words

8、the test is kidding

9、the man is true
10、#woood # #woooooood # AxyzxyzxyzxyzC
11、I bet this place is really spooky late at night! Misfortunes never come alone/single.
12、wd

13、wod
14、I shouldn't have lett so tast.

方法二:

[root@localhost opt]# awk ‘{print}’ test.txt 
awk: cmd. line:1: ‘{print}’
awk: cmd. line:1: ^ invalid char '�' in expression
[root@localhost opt]# awk '{print}' test.txt 
1、he was short and fat.
2、He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
4、The year ahead will test our political establishment to the limit.
5、PI=3.141592653589793238462643383249901429
6、a wood cross!
.......省略部分内容

示例2:输出第1~3行内容

方法一

[root@localhost opt]# awk 'NR==1,NR==3{print}' test.txt 
1、he was short and fat.
2、He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.

方法二

[root@localhost opt]# awk 'NR>=1&&NR<=3{print}' test.txt 
1、he was short and fat.
2、He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.

示例3:输出第1行、第3行内容

[root@localhost opt]# awk 'NR==1||NR==3{print}' test.txt 
1、he was short and fat.
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.

示例4:输出所有奇数行/偶数行 的内容

输出奇数行内容:

[root@localhost opt]# awk '(NR%2)==1{print}' test.txt
1、he was short and fat.
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
5、PI=3.141592653589793238462643383249901429
7、Actions speak louder than words
8、the test is kidding
9、the man is true
11、I bet this place is really spooky late at night! Misfortunes never come alone/single.

14、I shouldn't have lett so tast.

输出偶数行内容:

[root@localhost opt]# awk '(NR%2)==0{print}' test.txt
2、He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
4、The year ahead will test our political establishment to the limit.
6、a wood cross!

10、#woood # #woooooood # AxyzxyzxyzxyzC
12、wd
13、wod

示例5:输出以root 开头的行

[root@localhost opt]# awk '/^root/{print}' /etc/passwd
root:x:0:0:root:/root:/bin/bash

示例6:输出以nologin结尾的行

[root@localhost opt]# awk '/nologin$/{print}' /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin

示例7:统计以/bin/bash 结尾的行数,等同于 grep -c “/bin/bash$” etc/passwd

[root@localhost opt]# awk 'BEGIN{x=0};/\bin\/bash$/{x++};END{print x}' /etc/passwd
1

统计以空行分割的文本段落数:

[root@localhost opt]# awk 'BEGIN{RS=""};END{print NR}' /opt/test.txt 
4

需注意:sed 参照的是相对路径,而awk参照的是绝对路径
以上两个示例类似于for循环。

/\bin\/bash$/ 此段因为有多个“/”,所以要对/bin、/bash前加转义符表示/bin为普通“/”

通常我们使用“{}”时,在括号内写的时具体的操作

3.2.2 按字段输出文本

示例1:输出每行中(以空格或制表位分隔)的第3个字段

[root@localhost ~]# awk '{print $3}' /opt/test.txt 
short
wearing
is
ahead

cross!
louder

is

is
#woooooood
this



have

示例2: 输出每行中的第1、3个字段

[root@localhost ~]# awk '{print $1,$3}' /opt/test.txt 
1、he short
2、He wearing
3、the is
4、The ahead
5、PI=3.141592653589793238462643383249901429 
6、a cross!
7、Actions louder

8、the is

9、the is
10、#woood #woooooood
11、I this
12、wd

13、wod 
14、I have

示例3:输出密码为空的用户的shadow记录

[root@localhost ~]# awk -F: '$2="!!"{print}' /etc/shadow
root !!  0 99999 7   
bin !! 17110 0 99999 7   
daemon !! 17110 0 99999 7   
adm !! 17110 0 99999 7   
lp !! 17110 0 99999 7   
sync !! 17110 0 99999 7   
shutdown !! 17110 0 99999 7   
.......省略部分内容

示例4:输出以冒号分隔且第7个字段中包含/bash的行第1个字段

[root@localhost ~]# awk -F: '$7~"/bash"{print $1}' /etc/passwd
root
dings
wangwu
lisi

示例5 :输出包含8个字段且第1个字段中包含nfs的行的第1、2个字段

[root@localhost ~]# awk -F: '($1~"nfs")&&(NF=8){print $1,$2}' /etc/services
nfs             2049/tcp        nfsd shilp      # Network File System 
nfs             2049/udp        nfsd shilp      # Network File System 
nfs             2049/sctp       nfsd shilp      # Network File System 
netconfsoaphttp 832/tcp                 # NETCONF for SOAP over HTTPS 
netconfsoaphttp 832/udp                 # NETCONF for SOAP over HTTPS 
netconfsoapbeep 833/tcp                 # NETCONF for SOAP over BEEP 
netconfsoapbeep 833/udp                 # NETCONF for SOAP over BEEP 
nfsd-keepalive  1110/udp                # Client status info 
picknfs         1598/tcp                # picknfs 
picknfs         1598/udp                # picknfs 
.......省略部分内容

示例6:输出第7个字段既不为/bin/bash 也不为/sbin/nologin 的所有行

[root@localhost ~]# awk -F: '($7!="/bin/bash")&&($7!="/sbin/nologin"){print}' /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
3.2.3 通过管道、双引号调用Shell命令

示例1:调用wc -l 命令统计使用bash 的用户个数,等同于grep -c “bash$” /etc/passwd

[root@localhost ~]# awk -F: '/bash$/{print | "wc -l"}' /etc/passwd
4

示例2:调用w命令。并用来统计在线用户数

[root@localhost ~]# awk 'BEGIN{while("w" | getline) n++;{print n-2}}'
2

示例3:调用hostname,并输出当前的主机名

一般情况我们可以直接使用hostname~

[root@localhost opt]# hostname
localhost.localdomain

使用awk实现此命令:

[root@localhost opt]# awk 'BEGIN {"hostname"|getline ; print $0}' 
localhost.localdomain

四、sort 工具

4.1 sort工具概述

在linux 系统中,常用的文件排序工具有三种:sort、uniq、wc。本章将介绍前两种工具的方法。

sort 是一个以行为单位对文件内容进行排序的工具,也可以根据不同的数据类型来排序。例如数据和字符的牌局就不一样。sort命令的语法为”sort [选项]参数“。

常用选项

在这里插入图片描述

4.2 应用示例:

示例1:将/etc/passwd文件中的账号进行排序

[root@localhost opt]# sort /etc/passwd
abrt:x:173:173::/etc/abrt:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
chrony:x:995:991::/var/lib/chrony:/sbin/nologin
colord:x:997:995:User for 
.........省略部分内容

使用此排序方式会根据文件中第一个字母的顺序进行排序,若第一个字母相同,则匹配第二个字母顺序,以此类推。

示例2:将/etc/passwd 文件中第三列进行反向排序

[root@localhost opt]# sort -t ":" -rk 3 /etc/passwd
nobody:x:99:99:Nobody:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
libstoragemgmt:x:998:996:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
colord:x:997:995:User for colord:/var/lib/colord:/sbin/nologin
saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
chrony:x:995:991::/var/lib/chrony:/sbin/nologin
geoclue:x:994:989:User for geoclue:/var/lib/geoclue:/sbin/nologin
.........省略部分内容

以上匹配第三列的时候,会匹配第一个数字或者字母的大小,而不是匹配每行第三列的所有字符。

示例3: 将/etc/passwd文件中第三列进行排序,并将输出内容保存至user.txt文件中

[root@localhost opt]# sort -t':' -k 3 /etc/passwd -o /opt/user.txt
[root@localhost opt]# cat user.txt
root:x:0:0:root:/root:/bin/bash
dings:x:1000:1000:dings:/home/dings:/bin/bash
wangwu:x:1001:1001::/home/wangwu:/bin/bash
lisi:x:1002:1002::/home/lisi:/bin/bash
qemu:x:107:107:qemu user:/:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
........省略部分内容

五、uniq 工具

5.1 uniq工具概述

Uniq工具在Linux 系统中通常与sort命令结合使用,用于报告或者忽略文件中的重复行。

命令语法格式:uniq [选项] 参数

常用选项:

-c :进行计数

-d:仅显示重复行

-u:仅显示出现一次的行

5.2 常用示例

示例1:删除testfile文件中的重复行

创建文件:

[root@localhost opt]# cat testfile 
Linux10
Linux20
Linux30
Linux30
Linux40
Linux40
Linux40
Linux50
Linux50
Linux50
Linux60
Linux60
Centos6.5
Centos6.5
Centos7.3
Centos7.3
Centos7.3
Centos7.3

删除重复行:

[root@localhost opt]# uniq testfile 
Linux10
Linux20
Linux30
Linux40
Linux50
Linux60
Centos6.5
Centos7.3

示例2:删除testfile文件中的重复行,并在行首显示该行重复出现的次数~

[root@localhost opt]# uniq -c testfile 
      1 Linux10
      1 Linux20
      2 Linux30
      3 Linux40
      3 Linux50
      2 Linux60
      2 Centos6.5
      4 Centos7.3

示例3:查找testfile 文件中的重复行

[root@localhost opt]# uniq -d testfile 
Linux30
Linux40
Linux50
Linux60
Centos6.5
Centos7.3

总结

正则表达式中的文本编译器处理器中有很多琐碎但是相当重要的命令,在日常学习、工作中可以扩大我们可操作的范围。

发布了48 篇原创文章 · 获赞 46 · 访问量 6618

猜你喜欢

转载自blog.csdn.net/weixin_45726050/article/details/103386909