首页 > 系统相关 >Shell 编程之正则表达式与文本处理器

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

时间:2024-06-20 19:28:40浏览次数:14  
标签:sbin Shell 正则表达式 编程 sed test txt root nologin

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


在 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.5

CentOS 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.5

CentOS 6.5
CentOS 7.3

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

[root@bogon ~]# uniq -c testfile
      1 Linux 10
      1 Linux 20
      3 Linux 30
      1 CentOS 6.5
      1 
      2 CentOS 6.5
      3 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 编程中更好地利用正则表达式和文本处理器。

标签:sbin,Shell,正则表达式,编程,sed,test,txt,root,nologin
From: https://blog.csdn.net/m0_74860678/article/details/139840462

相关文章

  • shell编程规范与变量(shell脚本)
    一、Shell的介绍二、Shell的概述三、Shell的作用在一些复杂的 Linux 维护工作中,大量重复性的输入和交互操作不仅费时费力,而且容易出错,而编写一个恰到好处的 Shell 脚本程序,可以批量处理、自动化地完成一系列维护任务,大大减轻管理员的负担。常见的 Shell 解释器程序......
  • linux修改shell导致无法登录
    linux修改shell导致无法登录我之前在切换shell的时候执行了chsh-sbashroot结果后面就无法登录系统了,当时不知道怎么一回事,只能先放到一边了。今天没什么事,突然福至心灵,为什么不试试进到单用户模式里面修改shell呢?然后我就得到了这样的提示:chsh:PAM:Authenticationfailure......
  • 6.3实验五 数据库编程
    实验五 数据库编程一、实验目的熟悉并掌握嵌入式SQL编程、使用数据库访问接口技术实现对数据库的访问。二、实验要求熟悉使用嵌入式SQL编程访问数据库,熟悉VB中开发数据库应用程序的过程。三、实验步骤设计一个小型的数据库应用程序 1. 基本表建立(1)教师表建立Xum......
  • 深入理解AQS:Java并发编程中的核心组件
    目录AQS简介AQS的设计思路AQS的核心组成部分状态(State)同步队列(SyncQueue)条件队列(ConditionQueue)AQS的内部实现节点(Node)锁的获取与释放独占锁共享锁条件变量AQS的应用案例ReentrantLockCountDownLatchSemaphore总结参考文献AQS简介AbstractQueuedSynchronizer(AQ......
  • 管理 MySQL Shell 配置选项
    与任何工具一样,MySQLShell的开箱即用配置可能无法满足每个用户在任何情况下的需求。我们需要一种方法来轻松查看、更新和持续(如有必要)更改默认配置。有一条命令可以帮助我们管理MySQLShell配置。这条命令就是\option。 查看帮助MySQLlocalhostJS>\optionNAME......
  • shell运算符
    熟练掌握以上运算符能够自如的应对linux日常shell脚本需求。数值运算符只能用在数字上,不能用在其它数据类型上算数运算符基本就是常见的数学用到的计算:+、-、*、/、%需要注意的是默认情况下,shell不会直接进行算术运算,而是把"算术符号"当做"字符串"与两个变量的值连接在......
  • 如何证明数学中是根号2无理数,并且通过编程求解根号2的值
    1. 无理数和有理数的定义      实数可以简单的分为有理数和无理数,有理数都可以采用分数   (其中a和b都是互质的整数)表示;而无理数不可以使用分数表示,并且无理数是无限不循环小数。2. 根号2是无理数的证明过程目前常见的证明是无理数的证明方法是反证法,......
  • 不为人知的网络编程(十六):深入分析与解决TCP的RST经典异常问题
    本文由腾讯技术kernel分享,原题“TCP经典异常问题探讨与解决”,下文进行了排版和内容优化等。1、引言TCP的经典异常问题无非就是丢包和连接中断,在这里我打算与各位聊一聊TCP的RST到底是什么?现网中的RST问题有哪些模样?我们如何去应对和解决?本文将从TCP的RST技术原理、排查手段、......
  • 编程语言与字符编码
    聊聊在编程语言中的字符,例如Java‍一个字符占多少个字节?得看情况,不同编码下情况不同。这里引用肖国栋大佬的知乎回答:具体地讲,脱离具体的编码谈某个字符占几个字节是没有意义的。就好比有一个抽象的整数“42”,你说它占几个字节?这得具体看你是用byte,short,int,还是long来......
  • shell - 变量及数学计算
    变量声明#注意:以num=1为例,等号两边不能有空格#数字num=1#字符串str0=teststr1='test'str2="test"#字符串的三种声明方式是有区别的:#1.单引号中的内容回原样输出,不会转义,不会取值。#2.双引号中的内容输出,会转义,会取值。#3.没有引号和双引号效果一样。......