正则表达式(grep命令,egrep命令,sed命令,awk命令,sort工具,uniq工具)

正则表达式

基础正则表达式:grep命令

从 /opt/httpd.txt文件中查找出特定字符“the” 所在位置,不区分大小写

[root@localhost opt]# grep -n 'the' /opt/httpd.txt 

-n”表示显示行号、“-i”表示不区分大小写-n”表示显示行号、“-i”表示不区分大小写

img

从 /opt/httpd.txt文件中查找出特定字符“the” 所在位置
[root@localhost opt]# grep -in 'the' /opt/httpd.txt 

image-20200728002259192

[root@localhost opt]# grep -vn 'the' /opt/httpd.txt 

若反向选择,如查找不包含“the”字符的行,则需要通过grep命令的“-v”选项实现,并配合“-n”一起使用显示行号。

image-20200728002315886

(1) 利用中括号“[]”来查找集合字符

想要查找“shirt”与“short”这两个字符串时,可以发现这两个字符串均包含“sh” 与“rt”

“[]”中无论有几个字符,都仅代表一个字符,也就是说“[io]”表示匹配“i”或者“o”

同时查找到“shirt”与“short”这两个字符串
[root@localhost opt]# echo  "shirt" >> /opt/httpd.txt 

[root@localhost opt]# echo  "short" >> /opt/httpd.txt 

[root@localhost opt]# echo  "shart" >> /opt/httpd.txt 
[root@localhost ~]# grep -n 'sh[ioa]rt' /etc/passwd

image-20200728002331566

若要查找包含重复单个字符“oo”时,只需要执行以下命令即可。

image-20200728002350669

查找行首“^”与行尾字符“$

基础正则表达式包含两个定位元字符:“^”(行首)与“$”(行尾)

“^”符号在元字符集合“[]”符号内外的作用是不一样的,在“[]”符号内表示反向选择,在“[]”符号外则代表定位行首

若想查找以某一特定字符结尾的行则可以使用“$”定位符

查询以“the”字符串为行首的行

若查找“oo”前面不是“w”的字符串,只需要通过集合字符的反向选择“[^]”来实现该目的

image-20200728002402160

“a-z”表示小写字母,大写字母则通过“A-Z”表示。

若不希望“oo”前面存在小写字母,可以使用“grep -n[^a-z]oo’test.txt”命令实现

image-20200728002419051

查找包含数字的行可以通过*grep -n‘[0-9]’test.txt命令来实现。

image-20200728002427628

开头为0-9的

image-20200728002441030

查询以小写字母开头的行可以通过[a-z]规则来过滤,查询大写字母开头的行则使用[A-Z]”规则,若查询不以字母开头的行则使用“[a-zA-Z]”规则。

image-20200728002454048

因为小数点(.)在正则表达式中也是一个元字符(后面会讲到),所以在这里需要用转义字符“\”将具有特殊意义的字符转化成普通字符。

查询以小数点(.)结尾的行 [root@localhost opt]# grep -n ‘.$’ httpd.txt

image-20200728002509442

当查询空白行时,执行grep -n‘^$’test.txt命令即可。

image-20200728002744369

(1) 查找任意一个字符".“与重复字符”*"

表示固定字符

image-20200728002755004

“*”代表的是重复零个或多个前面的单字符

“o”表示拥有零个(即为空字符)或大于等于一个“o”的字符*

image-20200727111451491

执行以下命令即可查询以 w 开头 d 结尾,中间的字符可有可无的字符串

image-20200726091449323

“oo*”, 则第一个 o 必须存在,第二个 o 则是零个或多个 o,所以凡是包含 o、oo、ooo、ooo,等的资料都符合标准

查询以 w 开头 d 结尾,中间包含至少一个 o 的字符串,执行以下命令即可实现。

image-20200728002816424

执行以下命令即可查询任意数字所在行。

image-20200728002827296

查找连续字符范围{}

在上面的示例中,使用了“.”与“*”来设定零个到无限多个重复的字符,如果想要限制一个范围内的重复的字符串该如何实现呢?例如,查找三到五个 o 的连续字符,这个时候就需要使用基础正则表达式中的限定范围的字符“{}”。因为“{}”在 Shell 中具有特殊意义,所以在使用“{}”字符时,需要利用转义字符“\”,将“{}”字符转换成普通字符。“{}”字符的使用方法如下所示。

查询两个或以上 o 的字符

image-20200728002834971

查询以 w 开头以 d 结尾,中间包含 2~5 个 o 的字符串。

image-20200726092416092

查询以 w 开头以 d 结尾,中间包含 2 以上 o 的字符串

image-20200728002847250

元字符总结

元字符 作用
^ 匹配输入字符串的开始位置
$ 匹配输入字符串的结尾位置
. 匹配除“\r\n”之外的任何单个字符
\ 将下一个字符标记为特殊字符、原义字符、向后引用、八进制转义符。例如,‘n’匹配字符“n”。 ‘\n’匹配换行符。序列‘\ \’匹配“\”,而‘\ (’则匹配“(”
* 匹配前面的子表达式零次或多次。要匹配“*”字符,请使用“\ *”
[] 字符集合。匹配所包含的任意一个字符。例如,“[abc]”可以匹配“plain”中的“a”
[^] 赋值字符集合。匹配未包含的一个任意字符。例如,“[ ^abc ] ”可以匹配“plain”中“plin”中的任何一个字母
[n1-n2] 字符范围。匹配指定范围内的任意一个字符。例如,“[a-z]”可以匹配“a”到“z”范围内的任意一个小写字母字符。注意:只有连字符(-)在字符组内部,并且出现在两个字符之间时,才能表示字符的范围;如果出现在字符组的开头,则只能表示连字符本身
{n} n 是一个非负整数,匹配确定的 n 次。例如,“o\ {2\ }”不能匹配“Bob”中的“o”,但是能匹配“food”中的两个 o
{n,} n 是一个非负整数,至少匹配 n 次。例如,“o\ {2,\ }”不能匹配“Bob”中的“o”,但能匹配“foooood”中的所有 o。“o\ {1,\ }”等价于“o+”。“o\ {0,\ }”则等价于“o*”
{n,m} m 和n 均为非负整数,其中 n<=m,最少匹配 n 次且最多匹配 m 次

正则表达式总结

命令基本格式
grep -cinvABC 'word' filename
-c '行数'

-i '不区分大小写'

-n '显示行号'

-v '取反'

-r '遍历所有子目录'

-A '后面跟数字,过滤出符合要求的行以及下面n行'

-B '同上,过滤出符合要求的行以及上面n行'

-C '同上,同时过滤出符合要求的行以及上下各n行'

扩展正则表达式

egrep命令

通常情况下会使用基础正则表达式就已经足够了,但有时为了简化整个指令,需要使用范围更广的扩展正则表达式。

元字符 作用与示例
+ 作用:重复一个或者一个以上的前一个字符 示例:执行“egrep -n ‘wo+d’ test.txt”命令,即可查询"wood" “woood” "woooooood"等字符串
作用:零个或者一个的前一个字符 示例:执行“egrep -n ‘bes?t’ test.txt”命令,即可查询“bet”“best”这两个字符串
| 作用:使用或者(or)的方式找出多个字符 示例:执行“egrep -n ‘of
() 作用:查找“组”字符串 示例:“egrep -n ‘t(a
()+ 作用:辨别多个重复的组 示例:“egrep -n ‘A(xyz)+C’ test.txt”。该命令是查询开头的"A"结尾是"C",中间有一个以上的 "xyz"字符串的意思

示例:“egrep -n ‘A(xyz)+C’ test.txt”。该命令是查询开头的"A"结尾是"C",中间有一个以上的"xyz"字符串的意思

+ 作用:重复一个或者一个以上的前一个字符

示例:执行“egrep -n ‘wo+d’ test.txt”命令,即可查询"wood" “woood” "woooooood"等字符串

image-20200727112618261

? 作用:零个或者一个的前一个字符

image-20200728002952433

查看is|or|the ; 使用|或者(or)的方式找出多个字符

image-20200728002959967

image-20200728003012747

image-20200728003019946

sed工具使用方法

sed工具概述

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

sed的工作流程

读取——》执行——》显示三个过程

在所有的文件内容都被处理完成之前,上述过程将重复执行,直至所有内容被处理完

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

读取

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

执行

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

显示

发送修改后的内容到输出流。再发送数据后,模式空间将会被清空。

image-20200726002057113

通常情况下调用 sed 命令有两种格式

sed[选项] '操作' 参数
sed [选项] -f scriptfile 参数

常见的sed命令选项

选项 解释
-e 或–expression= 表示用指定命令或者脚本来处理输入的文本文件
-f 或–file= 表示用指定的脚本文件来处理输入的文本文件
-h 或–help 显示帮助
-n、–quiet 或 silent 表示仅显示处理后的结果
-i 直接编辑文本文件,直接编辑源文件

常见的操作

操作 解释
a 增加,在当前行下面增加一行指定内容
c 替换,将选定行替换为指定内容
d 删除,删除选定的行
i 插入,在选定行上面插入一行指定内容
p 打印,如果同时指定行,表示打印指定行;如果不指定行,则表示打印所有内容;如果有非打印字符,则以 ASCII 码输出。其通常与“-n”选项一起使用
s 替换,替换指定字符
y 字符转换

*用法示例*

[root@localhost opt]# sed -n 'p' a.txt   //输出所有内容,

1

2

3

4

5

6

7

8

9

0

[root@localhost opt]# sed -n '2p' a.txt //输出第 2 行

1

[root@localhost opt]# sed -n '3,5p' a.txt  //输出 3~5 行

2

3

4

[root@localhost opt]# sed -n 'p;n' a.txt  //输出所有奇数行,n 表示读入下一行资料

2

4

6

8

0

[root@localhost opt]# sed -n 'n;p' a.txt   //输出所有偶数行,n 表示读入下一行资料

1

3

5

7

9

[root@localhost opt]# sed -n '1,5{p;n}' a.txt //输出第 1~5 行之间的奇数行(第 1、3、5 行)

1

3

5

[root@localhost opt]# sed -n '1,5{n;p}' a.txt   //输出第 5行至文件尾之间的偶数行

2

4 

6

[root@localhost opt]# sed -n '/the/p' a.txt   //输出包含the 的行

the this

this the

[root@localhost opt]# sed -n '2,/the/p' a.txt   //输出从第 2行至第一个包含 the 的行

2

3

4

5

6

7

the this

[root@localhost opt]# sed -n '/the/=' a.txt   //输出包含the 的行所在的行号,等号(=)用来输出行号

8

10

[root@192 ~]# sed -n ‘/[0-9]/p’ /opt/httpd.txt 输出以数字

image-20200726101813459

以0-9开头的或结尾的

[root@localhost opt]# sed -n '/^[0-9]/p' httpd.txt 

12345678

9

[root@localhost opt]# sed -n '/[0-9]$/p' httpd.txt  //输出以数字结尾的行 
 
\#Listen 12.34.56.78:80

Listen 80

\#ServerName www.example.com:80

AddDefaultCharset UTF-8

12345678

?456789

9

//输出包含单词wood 的行,<、>代表单词边界

image-20200728003043812

删除符合条件的文本(d)

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

[root@localhost opt]# nl a.txt 

​     1	1

​     2	2

​     3	3

​     4	4

​     5	5

​     6	6

​     7	7

​     8	the this

​     9	8

​    10	this the

​    11	9

​    12	0

[root@localhost opt]# nl a.txt | sed '3d'  //删除第 3 行    不会影响源文件

​     1	1

​     2	2

​     4	4

​     5	5

​     6	6

​     7	7

​     8	the this

​     9	8

​    10	this the

​    11	9

​    12	0

[root@localhost opt]# nl a.txt | sed '3,$d'     //删除第 3 以后的行

​     1	1

​     2	2

[root@localhost opt]# nl a.txt | sed '/the/d'  //删除the的行

​     1	1

​     2	2

​     3	3

​     4	4

​     5	5

​     6	6

​     7	7

​     9	8

​    11	9

​    12	0


[root@localhost opt]# sed '/\.$/d' httpd.txt     //删除以"."结尾的行

\#

\# This is the main Apache HTTP server configuration file.  It contains the

\# In particular, see 

\# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>

\#

\# Do NOT simply read the instructions in here without understanding

\# what they do.  They're here only as hints or reminders.  If you are unsure

[root@localhost opt]# sed '/^$/d' httpd.txt     //删除所有空行

\#

\# This is the main Apache HTTP server configuration file.  It contains the

\# configuration directives that give the server its instructions.

\# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.

\# In particular, see 

\# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>

\# for a discussion of each configuration directive.




替换符合条件的文本

[root@localhost opt]# cat a.txt 

1

2

3

4

5

6

7

the this

8

this the

9

0

[root@localhost opt]# sed 's/the/THE/' a.txt    //将每行中的第一个the 替换为 THE

1

2

3

4

5

6

7

THE this

8

this THE

9

0

[root@localhost opt]# sed 's/l/L/3' a.txt  //将每行中的第 3 个 l 替换为 L

1  

2

3

4

5

6

7

the this

tollLlllk

8

this the

9

0

[root@localhost opt]# sed 's/l//g' a.txt    //将文件中的所有 l 删除(替换为空串)

1 

2

3

4

5

6

7

the this

tok

8

this the

9

0

[root@localhost opt]# sed 's/^/#/' a.txt    //在每行行首插入#号

\#1 

\#2

\#3

\#4

\#5

\#6

\#7

\#the this

\#tollllllk

\#8

\#this the

\#9

\#0

[root@localhost opt]# sed '/the/s/^/#/' a.txt   //在包含the 的每行行首插入#号

1

2

3

4

5

6

7

\#the this

tollllllk

8

\#this the

9

0

[root@localhost opt]# sed 's/l/#/g' a.txt   把l替换成#

1

2

3

4

5

6

7

the this

to######k

8

this the

9

0

迁移符合条件的文本

在使用 sed 命令迁移符合条件的文本时,常用到以下参数.

H:复制到剪贴板;

g、G:将剪贴板中的数据覆盖/追加至指定行;

w:保存为文件;

r:读取指定文件;

a:追加指定内容。

[root@localhost opt]# sed '/the/{H;d};12G' a.txt   //将第 the行内容转移至第 17 行后

1

2

3

4

5

6

7

tollllllk

8

9

the this

this the

0

[r

[root@localhost opt]# sed '/the/{H;d};$G' a.txt  //将包含the 的行迁移至文件末尾,{;}用于多个操作

1

2

3

4

5

6

7

tollllllk

8

9

0

the this

this the

[root@localhost opt]# sed '1,3{H;d};$G' a.txt   //将第 1~3 行内容转移至末尾

4

5

6

7

the this

tollllllk

8

this the

9

0

1

2

3

[root@localhost opt]# sed '/the/w out.txt' a.txt  //将文件 out.txt 的内容添加到包含 the 的每行以后

1

2

3

4

5

6

7

the this

tollllllk

8

this the

9

0

[root@localhost opt]# ls

 out.txt

[root@localhost opt]# cat out.txt 

the this

this the

\----------------------------------------

[root@localhost opt]# vim b.txt  

123456

[root@localhost opt]# sed '/the/r b.txt' a.txt    //在包含the 的每行后插入一个新行,内容为 123456

1

2

3

4

5

6

7

the this

123456

tollllllk

8

this the

123456

9

0

[root@localhost opt]# sed '3aNEW' a.txt           //在第 3 行后插入一个新行,内容为New

1

2

3

NEW

4

5

6

7

the this

tollllllk

8

this the

9

0

[root@localhost opt]# sed '/the/aNEW' a.txt   //在包含the 的每行后插入一个新行,内容为 New

1

2

3

4

5

6

7

the this

NEW

tollllllk

8

this the

NEW

9

0

[root@192 ~]# sed '3aNEW\nNWE2\nNEW3' a.txt    //在第 3 行后插入多行内容,中间的\n 表示换行

1

2

3

NEW

NWE2

NEW3

4

5

6

7

the this

NEW

tollllllk

8

this the

NEW

9

0

使用脚本编辑文件

[root@192 ~]# vim opt.zhang将第 1~3行内容转移至第 12 行后

1,3H

1,3d

12G

[root@192 ~]# sed -f opt.zhang a.txt 

4

5

6

7

the this

NEW

tollllllk

8

this the

1

2

3

NEW

9

0

sed 直接操作文件示例

  • 编写一个脚本,用来调整 vsftpd 服务配置:禁止匿名用户,但允许本地用户(也允许写入)
**(1)** **sed** **直接操作文件示例**

编写一个脚本,用来调整 vsftpd 服务配置,要求禁止匿名用户,但允许本地用户(也允许写入)。

root@localhost ~]# **vim local_only_ftp.sh**

\#!/bin/bash

\# 指定样本文件路径、配置文件路径

SAMPLE="/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/vsftpd.conf " CONFIG="/etc/vsftpd/vsftpd.conf"

\# 备份原来的配置文件,检测文件名为/etc/vsftpd/vsftpd.conf.bak 备份文件是否存在, 若不存在则使用 cp 命令进行文件备份

[ ! -e "$CONFIG.bak" ] && cp $CONFIG $CONFIG.bak # 基于样本配置进行调整,覆盖现有文件  -e判断是否存在不存这拷贝

sed -e '/^anonymous_enable/s/YES/NO/g' $SAMPLE > $CONFIG    

g是替换

sed -i -e '/^local_enable/s/NO/YES/g' -e '/^write_enable/s/NO/YES/g' $CONFIG grep "listen" $CONFIG || sed -i '$alisten=YES' $CONFIG     i直接编辑  -e 跟多条语句   

grep "listen" $CONFIG || sed -i '$alisten=YES' $CONFIG

a 是

\# 启动vsftpd 服务,并设为开机后自动运行

systemctl restart vsftpd systemctl enable vsftpd

[root@localhost ~]# **chmod +x local_only_ftp.sh

awk工具使用方法

1. awk 常见用法

通常情况下 awk 所使用的命令格式如下所示,其中,单引号加上大括号“{}”用于设置对数据进行的处理动作。awk 可以直接处理目标文件,也可以通过“-f”读取脚本对目标文件进行处理。

awk 选项 **’**模式或条件 {编辑指令}’ 文件 1 文件 2 … //过滤并输出文件中符合条件的内容

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

-f 且默认情况下字段的分隔符为空格或 tab 键

[root@localhost ~]# **awk -F ':' '{print $1,$3,$4}' /etc/passwd**

root 0 0

bin 1 1

daemon 2 2

……//省略部分内容

用$1、$2、$3…顺序地表示行(记录)中的不同字段,另外 awk 用$0 表示整个行(记录)

在上述示例中,awk 命令对/etc/passwd 文件的处理过程如图

image-20200728003110408

awk ‘{print}’ test.txt //输出所有内容,等同于 cat test.txt

image-20200728003117265

输出第 1~3 行内容

image-20200728003126928

awk 'NR==1||NR==3{print}'  /etc/passwd** //输出第 1 行、第 3 行内容

[root@192 ~]# awk 'NR==1,NR==3{print}' /etc/passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

[root@192 ~]# awk -F ":" 'NR==1,NR==3{print $1,$3}' /etc/passwd  输出1到3行的1到3列字段

root 0

bin 1

daemon 2

[root@192 ~]# awk -F ":" '(NR>=1)&&(NR<=3){print $1,$3}' /etc/passwd

root 0

bin 1

daemon 2

[root@192 ~]# awk -F ":" 'NR>=1&&NR<=3{print $1,$3}' /etc/passwd

root 0

bin 1

daemon 2

[root@192 ~]# awk -F ":" 'NR==1||NR==3{print $1,$3}' /etc/passwd 只输出1和3行

root 0

daemon 2

[root@192 ~]# awk -F ":" 'NR%2==1{print $1,$3}' /etc/passwd   //输出所有奇数行的内容

root 0

daemon 2

lp 4

shutdown 6

mail 8

games 12

nobody 99

dbus 81

libstoragemgmt 998

rpc 32

saslauth 995

rtkit 172

[root@192 ~]# awk -F ":" 'NR%2==0{print $1,$3}' /etc/passwd  //输出所有偶数行的内容

bin 1

adm 3

sync 5

halt 7

operator 11

ftp 14

systemd-network 192

polkitd 999

colord 997

gluster 996

abrt 173

pulse 171

unbound 994

rpcuser 29

[root@192 ~]# awk '/^root/{print}' /etc/passwd   //输出以root 开头的行**awk** 

root:x:0:0:root:/root:/bin/bash

[root@192 ~]# awk '/nologin$/{print}' /etc/passwd   //输出以 nologin 结尾的行

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

**awk 'BEGIN {x=0};/\/bin\/bash$/{x++};END {print x}' /etc/passwd**

//统计以/bin/bash 结尾的行数,等同于 grep -c "/bin/bash$" /etc/passwd

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

25

[root@192 ~]# grep -c "/bin/bash$" /etc/passwd

25

**awk 'BEGIN{RS=""};END{print NR}' /etc/squid/squid.conf**

//统计以空行分隔的文本段落数

**awk '{print $3}' test.txt** //输出每行中(以空格或制表位分隔)的第 3 个字段

**awk '{print $1,$3}' test.txt** //输出每行中的第 1、3 个字段

[root@192 opt]# awk -F ":" '$3==0{print $1,$7}' /etc/passwd

root /bin/bash

[root@192 opt]# awk 'BEGIN{FS=":"};$3=="0"{print};' /etc/passwd  //输出以冒号分隔且第 7 个字段中包含/bash 的行的第 1 个字段

root:x:0:0:root:/root:/bin/bash

[root@192 opt]# awk -F ":" '$7~"/bash"{print $1}' /etc/passwd

root

tange

zhang

tam

tom

com1

com2

com3

[root@192 opt]# awk '($1~"nfs")&&(NF==8){print $1,$2}' /etc/services  //输出包含 8 个字段且第 1 个字段中包含 nfs 的行的第 1、2 个字段

nfs 2049/tcp

nfs 2049/udp

nfs 2049/sctp

netconfsoaphttp 832/tcp

netconfsoaphttp 832/udp

netconfsoapbeep 833/tcp

netconfsoapbeep 833/udp

[root@192 opt]# awk -F ":" '($7!="/bin/bash")&&($7!="/sbin/nologin"){print}' /etc/passwd  //输出第 7 个字段既不为/bin/bash 也不为/sbin/nologin 的所有行

sync:x:5:0:sync:/sbin:/bin/sync

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

[root@192 opt]# awk -F: '/bash$/{print | "wc -l"}' /etc/passwd  //调用wc -l 命令统计使用 bash 的用户个数,等同于 grep -c "bash$" /etc/passwd

25

**[root@192 opt]# awk 'BEGIN {while ("w" | getline) n++ ; {print n-2}}'**    //调用w 命令,并用来统计在线用户数

**5**

**getline判断下面有没有行**

-2是前2行不是

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

zhang

sort工具

命令 解释
-b 忽略每行前面的空格
-M 按照月份进行排序
-n 按照数字进行排序
-r 反向排序
-u 等同于 uniq,表示相同的数据仅显示一行
-t 指定分隔符,默认使用[Tab]键分隔
-o <输出文件>:将排序后的结果转存至指定文件
-k 指定排序区域
-f 忽略大小写

将/etc/passwd 文件中的账号进行排序。

image-20200727121215660

将/etc/passwd 文件中第三列进行反向排序。

image-20200727121451775

将/etc/passwd 文件中第三列进行反向排序。

image-20200727121615941

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

image-20200727121748801

uniq工具

Uniq 工具在 Linux 系统中通常与 sort 命令结合使用,用于报告或者忽略文件中的重复行
Ø -c:进行计数;

Ø -d:仅显示重复行;

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


删除 centos 文件中的重复行。

[root@localhost opt]# cat cetos.txt 
centos5
centos5
centos5
centos5
centos6
centos6
centos6
centos8
centos8
centos8
centos9
centos9
centos9
[root@localhost opt]# uniq cetos.txt  删除 centos 文件中的重复行。
centos5
centos6
centos8
centos9
[root@localhost opt]# uniq -c cetos.txt 删除 centos 文件中的重复行,并在行首显示该行重复出现的次数
      4 centos5
      3 centos6
      3 centos8
      3 centos9
[root@localhost opt]# uniq -d cetos.txt  查找 testfile 文件中的重复行
centos5
centos6
centos8
centos9

tr工具

tr 命令常用来对来自标准输入的字符进行替换、压缩和删除。可以将一组字符替换之后变成另一组字符,经常用来编写优美的单行命令,作用很强大。

tr 具体的命令语法格式为:

-c:取代所有不属于第一字符集的字符;
-d:删除所有属于第一字符集的字符;
-s:把连续重复的字符以单独一个字符表示;
-t:先删除第一字符集较第二字符集多出的字符。 
[root@localhost opt]# echo "KGC" | tr 'A-Z' 'a-z'将输入字符由大写转换为小写。
kgc
[root@localhost opt]# echo "thissss isa text linnnnnnne." | tr -s 'sn'压缩输入中重复的字符。
this isa text line.
[root@localhost opt]# echo 'hello world' | tr -d 'od'删除字符串中某些字符。
hell wrl

猜你喜欢

转载自blog.csdn.net/weixin_47151717/article/details/107627628