当前位置: 首页 > news >正文

Shell 编程之正则表达式与文本处理器

Shell 编程之正则表达式与文本处理器

  • 一、正则表达式
    • 基础正则表达式
    • 元字符总结
  • 二、文本处理器
    • sed 工具
    • awk工具
    • sort工具
    • uniq工具
    • tr工具
  • 三、总结

在 Shell 编程中,正则表达式(Regular Expression)和文本处理器(如 grep, sed, awk 等)是两个极其重要的工具,它们允许我们以复杂且高效的方式搜索、处理和操作文本数据。本博客将简要介绍 Shell 编程中正则表达式的使用,并展示如何使用 grep, sed, 和 awk 这三个强大的文本处理器。

一、正则表达式

基础正则表达式

  • “-n”表示显示行号
  • “-i”表示不区分大小写
  • 反向选择,“-v”选项实现,并配合“-n”一起使用显示行号

基础正则表达式示例
下面的操作需要提前准备一个名为 test.txt 的测试文件,文件具体内容如下所示。

[root@bogon ~]# cat test.txt 
he was short and fat.
He was wearing a blue polo shirt with black pants.
The home of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword.
The year ahead will test our political establishment to the limit.
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words#woood #
#woooooood #
AxyzxyzxyzxyzC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.

查找特定字符

# 文件中查找出特定字符“the”所在位置,并显示行号
[root@bogon ~]# grep -n 'the' test.txt
4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword.
6:The year ahead will test our political establishment to the limit.
# 文件中查找出特定字符“the”所在位置,显示行号,不区分大小写
[root@bogon ~]# grep -in 'the' test.txt
3:The home of Football on BBC Sport online.
4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword.
6:The year ahead will test our political establishment to the limit.

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

# 其中“[]”中无论有几个字符, 都仅代表一个字符,也就是说“[io]”表示匹配“i”或者“o”
[root@bogon ~]# grep -n 'sh[io]rt' test.txt
1:he was short and fat.
2:He was wearing a blue polo shirt with black pants.
# 过滤以 w 开头的字符
[root@bogon ~]# grep -n '[w]oo' test.txt
8:a wood cross!
11:#woood #
12:#woooooood #
# 过滤不以 w 开头的字符
[root@bogon ~]# grep -n '[^w]oo' test.txt    # 反向选择“[^]”
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
11:#woood #
12:#woooooood #
14:I bet this place is really spooky late at night!

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

  • “^”符号在元字符集合“[]”符号内外的作用是不一样的,在“[]”符号内表示反向选择,在“[]” 符号外则代表定位行首
  • 反之,若想查找以某一特定字符结尾的行则可以使用“$”定位符。
# 查询以“the”字符串为行首的行
[root@bogon ~]# grep -n '^the' test.txt
4:the tongue is boneless but it breaks bones.12!
# 查询以小写字母开头的行
[root@bogon ~]# grep -n '^[a-z]' test.txt
1:he was short and fat.
4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword.
8:a wood cross!
# 查询以大写字母开头的行
[root@bogon ~]# grep -n '^[A-Z]' test.txt
2:He was wearing a blue polo shirt with black pants.
3:The home of Football on BBC Sport online.
6:The year ahead will test our political establishment to the limit.
7:PI=3.141592653589793238462643383249901429
9:Actions speak louder than words
13:AxyzxyzxyzxyzC
14:I bet this place is really spooky late at night!
15:Misfortunes never come alone/single.
16:I shouldn't have lett so tast.
# 查询不以字母开头的行
[root@bogon ~]# grep -n '^[^a-zA-Z]' test.txt
11:#woood #
12:#woooooood #

查找任意一个字符“.”与重复字符“*”

  • 若想要查询 oo、ooo、ooooo 等资料, 则需要使用星号(*)元字符。
  • 但需要注意的是,“*”代表的是重复零个或多个前面的单字符。
  • “o*”表示拥有零个(即为空字符)或大于等于一个“o”的字符
# 查找“w??d”的字符串,即共有四个字符,以 w 开头 d 结尾
[root@localhost ~]# grep -n 'w..d' test.txt
5:google is the best tools for search keyword.
8:a wood cross!
9:Actions speak louder than words
# 查询包含至少两个 o 以上的字符串
[root@localhost ~]# grep -n 'ooo*' test.txt 
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
8:a wood cross!
11:#woood #
12:#woooooood #
14:I bet this place is really spooky late at night!
# 因为允许空字符,所以执行“grep -n 'o*' test.txt”命令会将文本中所有的内容都输出打印
[root@localhost ~]# grep -n 'o*' test.txt
1:he was short and fat.
2:He was wearing a blue polo shirt with black pants.
3:The home of Football on BBC Sport online.
4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword.
6:The year ahead will test our political establishment to the limit.
7:PI=3.141592653589793238462643383249901429
8:a wood cross!
9:Actions speak louder than words
10:
11:#woood #
12:#woooooood #
13:AxyzxyzxyzxyzC
14:I bet this place is really spooky late at night!
15:Misfortunes never come alone/single.
16:I shouldn't have lett so tast.# 查询以 w 开头 d 结尾,中间包含至少一个 o 的字符串
[root@localhost ~]#  grep -n 'woo*d' test.txt
8:a wood cross!
11:#woood #
12:#woooooood #

查找连续字符范围“{}”

  • 因为“{}”在 Shell 中具有特殊意义,所以在使用“{}”字符时,需要利用转义字符“\”,将“{}”字符转换成普通字符。
# 查询两个 o 的字符
[root@localhost ~]# grep -n 'o\{2\}' test.txt
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
8:a wood cross!
11:#woood #
12:#woooooood #
14:I bet this place is really spooky late at night!
# 查询以 w 开头以 d 结尾,中间包含 2~5 个 o 的字符串
[root@localhost ~]# grep -n 'wo\{2,5\}d' test.txt
8:a wood cross!
11:#woood #
# 查询以 w 开头以 d 结尾,中间包含 2 个或 2 个以上 o 的字符串
[root@localhost ~]# grep -n 'wo\{2,\}d' test.txt
8:a wood cross!
11:#woood #
12:#woooooood #

元字符总结

基础正则表达式常见元字符
元字符作用
^匹配输入字符串的开始位置。除非在方括号表达式中使用,表示不包含该字符集合。要匹配“^” 字符本身,请使用“\^”
$匹配输入字符串的结尾位置。如果设置了RegExp 对象的 Multiline 属性,则“$”也匹配‘\n’或‘\r’。要匹配“$”字符本身,请使用“\$”
.匹配除“\r\n”之外的任何单个字符
\反斜杠,又叫转义字符,去除其后紧跟的元字符或通配符的特殊意义
*匹配前面的子表达式零次或多次。要匹配“*”字符,请使用“\*”
[]字符集合。匹配所包含的任意一个字符。例如,“[abc]”可以匹配“plain”中的“a”
[^]赋值字符集合。匹配未包含的一个任意字符。例如,“[^abc]”可以匹配“plain”中任何一个字母
[n1-n2]字符范围。匹配指定范围内的任意一个字符。例如,“[a-z]”可以匹配“a”到“z”范围内的任意一个小写字母字符。
{n}n 是一个非负整数,匹配确定的 n 次。例如,“o{2}”不能匹配“Bob”中的“o”,但是能匹配“food”中的“oo”
{n,}n 是一个非负整数,至少匹配 n 次。例如,“o{2,}”不能匹配“Bob”中的“o”,但能匹配“foooood”中的所有o。“o{1,}”等价于“o+”。“o{0,}”则等价于“o*”
{n,m}m 和 n 均为非负整数,其中 n<=m,最少匹配 n 次且最多匹配m 次

扩展正则表达式

扩展正则表达式常见元字符
元字符作用与示例
+作用:重复一个或者一个以上的前一个字符 示例:执行“egrep -n 'wo+d' test.txt”命令,即可查询"wood" "woood" "woooooood"等字符串
?作用:零个或者一个的前一个字符 示例:执行“egrep -n 'bes?t' test.txt”命令,即可查询“bet”“best”这两个字符串
|作用:使用或者(or)的方式找出多个字符 示例:执行“egrep -n 'of|is|on' test.txt”命令即可查询"of"或者"if"或者"on"字符串
()作用:查找“组”字符串 示例:“egrep -n 't(a|e)st' test.txt”。“tast”与“test”因为这两个单词的“t”与“st”是重复的,所以将“a”与“e”列于“()”符号当中,并以“|”分隔,即可查询"tast"或者"test"字符串
()+作用:辨别多个重复的组 示例:“egrep -n 'A(xyz)+C' test.txt”。该命令是查询开头的"A"结尾是"C",中间有一个以上的"xyz"字符串的意思

二、文本处理器

sed 工具

sed工具概述

  • 文本处理工具,读取文本内容,根据指定的条件进行处理,如删除,替换,添加等
  • 可在无交互的情况下实现相当复杂的文本处理操作
  • 被广泛应用于Shell脚本,已完成自动化处理任务
  • sed依赖于正则表达式
  • 工作原理
    • 读取➡执行➡显示
      • 读取:sed 从输入流(文件、管道、标准输入)中读取一行内容并存储到临时的缓冲区中(又称模式空间,pattern space)。
      • 执行:默认情况下,所有的 sed 命令都在模式空间中顺序地执行,除非指定了行的地址,否则 sed 命令将会在所有的行上依次执行。
      • 显示:发送修改后的内容到输出流。在发送数据后,模式空间将会被清空。

sed 命令常见用法

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

常见的sed命令选项

  • -e 或–expression=: 表示用指定命令或者脚本来处理输入的文本文件。
  • -f 或–file=: 表示用指定的脚本文件来处理输入的文本文件。
  • -h 或–help: 显示帮助。
  • -n、–quiet 或 silent: 表示仅显示处理后的结果。
  • -i: 直接编辑文本文件。
  • a: 增加,在当前行下面增加一行指定内容。
  • c: 替换,将选定行替换为指定内容。
  • d: 删除,删除选定的行。
  • i: 插入,在选定行上面插入一行指定内容。
  • p: 打印,如果同时指定行,表示打印指定行;如果不指定行,则表示打印所有内 容;如果有非打印字- 符,则以 ASCII 码输出。其通常与“-n”选项一起使用。
  • s: 替换,替换指定字符。
  • y: 字符转换
  • =: 显示行号

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

# 显示打印所有内容,类似于 cat 查看
[root@bogon ~]# sed -n 'p' test.txt 
he was short and fat.
He was wearing a blue polo shirt with black pants.
The home of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword.
The year ahead will test our political establishment to the limit.
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words#woood #
#woooooood #
AxyzxyzxyzxyzC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
# 显示打印第 1 行
[root@bogon ~]# sed -n '1p' test.txt 
he was short and fat.
# 显示打印第 3 行
[root@bogon ~]# sed -n '3p' test.txt 
The home of Football on BBC Sport online.
# 显示打印第 3 - 5 行
[root@bogon ~]# sed -n '3,5p' test.txt 
The home of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword.
# 显示打印所有带 the 的行 
[root@bogon ~]# sed -n '/the/p' test.txt 
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword.
The year ahead will test our political establishment to the limit.
# 显示打印数字
[root@bogon ~]# sed -n '/[0-9]$/p' test.txt 
PI=3.141592653589793238462643383249901429
# 显示行号
[root@bogon ~]# sed -n '/[0-9]$/=' test.txt 
7

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

nl test.txt | sed '3d'        # 删除第 3 行nl test.txt | sed '3,5d'      # 删除第 3~5 行nl test.txt |sed '/cross/d'   # 删除包含 cross 的行,原本的第 8 行被删除;如果要删除不包含 cross 的行,用!符号表示取反操作, 如'/cross/!d'sed '/^[a-z]/d' test.txt      # 删除以小写字母开头的行sed '/\.$/d' test.txt         # 删除以"."结尾的行sed '/^$/d' test.txt          # 删除所有空行sed -e'/^$/{n;/^$/d}' test.txt    # 删除重复的空行

替换符合条件的文本

  • 在使用 sed 命令进行替换操作时需要用到 s(字符串替换)
  • c(整行/整块替换)
  • y(字符转换)命令选项
sed 's/the/THE/' test.txt        # 将每行中的第一个the 替换为 THE
sed 's/l/L/2' test.txt            # 将每行中的第 2 个 l 替换为 L
sed 's/the/THE/g' test.txt        # 将文件中的所有the 替换为 THE
sed 's/o//g' test.txt            # 将文件中的所有o 删除(替换为空串)
sed 's/^/#/' test.txt            # 在每行行首插入#号
sed '/the/s/^/#/' test.txt        # 在包含the 的每行行首插入#号
sed 's/$/EOF/' test.txt            # 在每行行尾插入字符串EOF
sed '3,5s/the/THE/g' test.txt    # 将第 3~5 行中的所有 the 替换为 THE
sed '/the/s/o/O/g' test.txt        # 将包含the 的所有行中的 o 都替换为 O

迁移符合条件的文本

  • H: 复制到剪贴板;
  • g、G: 将剪贴板中的数据覆盖/追加至指定行;
  • w: 保存为文件;
  • r: 读取指定文件;
  • a: 追加指定内容。
sed '/the/{H;d};$G' test.txt            # 将包含the 的行迁移至文件末尾,{;}用于多个操作
sed '1,5{H;d};17G' test.txt                # 将第 1~5 行内容转移至第 17 行后
sed '/the/w out.file' test.txt            # 将包含the 的行另存为文件 out.file
sed '/the/r /etc/hostname' test.txt        # 将文件/etc/hostname 的内容添加到包含 the 的每行以后
sed '3aNew' test.txt                    # 在第 3 行后插入一个新行,内容为New
sed '/the/aNew' test.txt                # 在包含the 的每行后插入一个新行,内容为 New
sed '3aNew1\nNew2' test.txt                # 在第 3 行后插入多行内容,中间的\n 表示换行

使用脚本编辑文件

  • 使用 sed 脚本将多个编辑指令存放到文件中(每行一条编辑指令),通过“-f”选项来调用。
sed '1,5{H;d};17G' test.txt        # 将第 1~5 行内容转移至第 17 行后

sed 直接操作文件示例

  • 编写一个脚本,用来调整 vsftpd 服务配置,要求禁止匿名用户,但允许本地用户(也允许写入)
[root@localhost ~]# vi 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 
# 基于样本配置进行调整,覆盖现有文件
sed -e '/^anonymous_enable/s/YES/NO/g' $SAMPLE > $CONFIG
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 
# 启动vsftpd 服务,并设为开机后自动运行
[root@localhost ~]# systemctl restart vsftpd 
[root@localhost ~]# systemctl enable vsftpd
[root@localhost ~]# chmod +x local_only_ftp.sh

awk工具

awk工作原理

  • 逐行读取文本,默认以空格为分隔符进行分隔,将分隔所得的各个字段保存到内建变量中
    在这里插入图片描述
    awk 常见用法
  • awk 执行结果可以通过 print 的功能将字段数据打印显示。
  • 在使用 awk 命令的过程中,可以使用逻辑操作符“&&”表示“与”、“||” 表示“或”、“!”表示“非”;
  • 还可以进行简单的数学运算,如+、-、*、/、%、^分别表示加、减、乘、除、取余和乘方
awk 选项 '模式或条件 {编辑指令}' 文件 1 文件 2# 过滤并输出文件中符合条件的内容
awk   -f   脚本文件 文件 1 文件 2# 从脚本中调用编辑指令,过滤并输出内容
  • FS:指定每行文本的字段分隔符,默认为空格或制表位。
  • NF:当前处理的行的字段个数。
  • NR:当前处理的行的行号(序数)。
  • $0:当前处理的行的整行内容。
  • $n:当前处理行的第 n 个字段(第 n 列)。
  • FILENAME:被处理的文件名。
  • RS:数据记录分隔,默认为\n,即每行为一条记录。

用法示例
按行输出文本

awk '{print}' test.txt                    # 输出所有内容,等同于 cat test.txt
awk '{print $0}' test.txt                # 输出所有内容,等同于 cat test.txt
awk 'NR==1,NR==3{print}' test.txt        # 输出第 1~3 行内容
awk '(NR>=1)&&(NR<=3){print}' test.txt    # 输出第 1~3 行内容
awk 'NR==1||NR==3{print}' test.txt        # 输出第 1 行、第 3 行内容
awk '(NR%2)==1{print}' test.txt           # 输出所有奇数行的内容
awk '(NR%2)==0{print}' test.txt            # 输出所有偶数行的内容
awk '/^root/{print}' /etc/passwd        # 输出以root 开头的行
awk '/nologin$/{print}' /etc/passwd        # 输出以 nologin 结尾的行
# 统计以/bin/bash 结尾的行数,等同于 grep -c "/bin/bash$" /etc/passwd
awk 'BEGIN {x=0};/\/bin\/bash$/{x++};END {print x}' /etc/passwd
# 统计以空行分隔的文本段落数
awk 'BEGIN{RS=""};END{print NR}' /etc/squid/squid.conf    

按字段输出文本

awk '{print $3}' test.txt                            # 输出每行中(以空格或制表位分隔)的第 3 个字段
awk '{print $1,$3}' test.txt                        # 输出每行中的第 1、3 个字段
awk -F ":" '$2==""{print}' /etc/shadow              # 输出密码为空的用户的shadow 记录
awk 'BEGIN {FS=":"}; $2==""{print}' /etc/shadow     # 输出密码为空的用户的shadow 记录
awk -F ":" '$7~"/bash"{print $1}' /etc/passwd       # 输出以冒号分隔且第 7 个字段中包含/bash 的行的第 1 个字段
# 输出包含 8 个字段且第 1 个字段中包含 nfs 的行的第 1、2 个字段
awk '($1~"nfs")&&(NF==8){print $1,$2}' /etc/services   
# 输出第 7 个字段既不为/bin/bash 也不为/sbin/nologin 的所有行
awk -F ":" '($7!="/bin/bash")&&($7!="/sbin/nologin"){print}' /etc/passwd    

通过管道、双引号调用 Shell 命令

# 调用wc -l 命令统计使用 bash 的用户个数,等同于 grep -c "bash$" /etc/passwd
awk -F: '/bash$/{print | "wc -l"}' /etc/passwd
# 调用w 命令,并用来统计在线用户数
awk 'BEGIN {while ("w" | getline) n++ ; {print n-2}}'
# 调用hostname,并输出当前的主机名
awk 'BEGIN { "hostname" | getline ; print $0}'

sort工具

  • 在 Linux 系统中,常用的文件排序工具有三种:sort、uniq、wc
    • -f:忽略大小写;
    • -b:忽略每行前面的空格;
    • -M:按照月份进行排序;
    • -n:按照数字进行排序;
    • -r:反向排序;
    • -u:等同于 uniq,表示相同的数据仅显示一行;
    • -t:指定分隔符,默认使用[Tab]键分隔;
    • -o <输出文件>:将排序后的结果转存至指定文件;
    • -k:指定排序区域。

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

[root@bogon ~]# sort /etc/passwd
adm:x:3:4:adm:/var/adm:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin

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

[root@bogon ~]# sort -t ':' -rk 3 /etc/passwd 
nobody:x:99:99:Nobody:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sync:x:5:0:sync:/sbin:/bin/sync
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
root:x:0:0:root:/root:/bin/bash

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

[root@bogon ~]# sort -t ':' -k 3 /etc/passwd -o user.txt
[root@bogon ~]# cat user.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/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
systemd-network:x:192:192:systemd Network Management:/:/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
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin

uniq工具

  • Uniq 工具在 Linux 系统中通常与 sort 命令结合使用,用于报告或者忽略文件中的重复行
  • 具体的命令语法格式
uniq [选项] 参数
  • -c:进行计数;
  • -d:仅显示重复行;
  • -u:仅显示出现一次的行。

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

[root@bogon ~]# cat testfile
Linux 10
Linux 20
Linux 30
Linux 30
Linux 30
CentOS 6.5CentOS 6.5
CentOS 6.5
CentOS 7.3
CentOS 7.3
CentOS 7.3
[root@bogon ~]# uniq testfile
Linux 10
Linux 20
Linux 30
CentOS 6.5CentOS 6.5
CentOS 7.3

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

[root@bogon ~]# uniq -c testfile1 Linux 101 Linux 203 Linux 301 CentOS 6.51 2 CentOS 6.53 CentOS 7.3

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

[root@bogon ~]# uniq -d testfile
Linux 30
CentOS 6.5
CentOS 7.3

tr工具

  • tr 命令常用来对来自标准输入的字符进行替换、压缩和删除。可以将一组字符替换之后变成另一组字符,经常用来编写优美的单行命令,作用很强大
  • tr 具体的命令语法格式为
tr [选项] [参数]
  • -c:取代所有不属于第一字符集的字符;
  • -d:删除所有属于第一字符集的字符;
  • -s:把连续重复的字符以单独一个字符表示;
  • -t:先删除第一字符集较第二字符集多出的字符。

示例 1:将输入字符由大写转换为小写。

[root@bogon ~]# echo "KGC" | tr 'A-Z' 'a-z'
kgc

示例 2:压缩输入中重复的字符

[root@bogon ~]# echo "thissss isa text linnnnnnne." | tr -s 'sn'
this isa text line.

示例 3:删除字符串中某些字符

[root@bogon ~]# echo 'hello world' | tr -d 'od'
hell wrl

三、总结

正则表达式和文本处理器是 Shell 编程中不可或缺的工具。通过掌握这些工具,我们可以更高效地处理和分析文本数据。在实际应用中,我们可能需要根据具体需求组合使用这些工具,以达到最佳效果。希望本博客能为你提供一个良好的起点,让你在 Shell 编程中更好地利用正则表达式和文本处理器。

相关文章:

  • clickhouse学习笔记(四)库、表、分区相关DDL操作
  • 如何解决虚拟仿真教学中的设备限制和卡顿问题?|点量云流技术解决方案
  • performance_schema.events_statements_current
  • MavenPlus插件的基础功能完善
  • JAVA面试(六)
  • JAVA语言开发的一套(智慧工地监管系统源码)让工地变得更加“聪明”
  • 接口联调测试工作总结
  • Python 设计模式(第2版) -- 第二部分(结构型模式)
  • Java启动jar设置内存分配详解
  • JWT整合Gateway实现鉴权(RSA与公私密钥工具类)
  • 示例:WPF中DataGrid简单设置合并列头
  • 数据结构和算法之复杂度比较
  • 数据治理:如何通过优化数据提取流程提高数据分析效果
  • 使用 Python 中的美丽汤进行网络数据解析的完整指南
  • python从入门到精通6:变量类型
  • [译]Python中的类属性与实例属性的区别
  • Bytom交易说明(账户管理模式)
  • Java 多线程编程之:notify 和 wait 用法
  • java多线程
  • Just for fun——迅速写完快速排序
  • vue-cli3搭建项目
  • Webpack入门之遇到的那些坑,系列示例Demo
  • 从重复到重用
  • 二维平面内的碰撞检测【一】
  • 关键词挖掘技术哪家强(一)基于node.js技术开发一个关键字查询工具
  • 解决iview多表头动态更改列元素发生的错误
  • 人脸识别最新开发经验demo
  • 用mpvue开发微信小程序
  • ​低代码平台的核心价值与优势
  • ​人工智能之父图灵诞辰纪念日,一起来看最受读者欢迎的AI技术好书
  • # C++之functional库用法整理
  • #《AI中文版》V3 第 1 章 概述
  • #Linux(make工具和makefile文件以及makefile语法)
  • #LLM入门|Prompt#2.3_对查询任务进行分类|意图分析_Classification
  • #鸿蒙生态创新中心#揭幕仪式在深圳湾科技生态园举行
  • ( 10 )MySQL中的外键
  • (7)摄像机和云台
  • (webRTC、RecordRTC):navigator.mediaDevices undefined
  • (二)构建dubbo分布式平台-平台功能导图
  • (非本人原创)我们工作到底是为了什么?​——HP大中华区总裁孙振耀退休感言(r4笔记第60天)...
  • (附源码)ssm教师工作量核算统计系统 毕业设计 162307
  • (循环依赖问题)学习spring的第九天
  • (一)Java算法:二分查找
  • (一)u-boot-nand.bin的下载
  • (转)MVC3 类型“System.Web.Mvc.ModelClientValidationRule”同时存在
  • .md即markdown文件的基本常用编写语法
  • .NET Core 控制台程序读 appsettings.json 、注依赖、配日志、设 IOptions
  • .NET Core工程编译事件$(TargetDir)变量为空引发的思考
  • .net framework4与其client profile版本的区别
  • .NET MVC 验证码
  • .net Signalr 使用笔记
  • .NET 使用配置文件
  • .NET/C# 利用 Walterlv.WeakEvents 高性能地定义和使用弱事件
  • .net访问oracle数据库性能问题
  • [ vulhub漏洞复现篇 ] ThinkPHP 5.0.23-Rce