Linux 文本处理实战手册

发布于:2025-09-03 ⋅ 阅读:(17) ⋅ 点赞:(0)

一、为什么要掌握这些命令?

本手册将 Linux/Unix 运维与开发中最常用的“筛选、排序、替换”技能浓缩为两份口袋指南:先用 15 分钟掌握 cut、sort、uniq、tr 四个小工具,学会按列截取、排序去重及字符级清洗,并附带可直接套用的组合面试题与实战脚本;再深入 sed,从模式空间原理到增删改查、迁移、就地编辑及生产脚本,教你用一行 sed 解决原本需要写脚本的问题,新手可入门,老手可备忘,打开终端敲一遍就能体会“所谓高效运维,其实就是把文本玩到极致”。

二、四小工具:cut / sort / uniq / tr

命令 作用 典型场景
cut 按列/字符截取 取用户名、取 IP
sort 排序 按 UID 排序、去重前排序
uniq 去重计数 统计访问 IP、登录用户
tr 字符级替换/删除/压缩 大小写转换、删除回车

2.1 cut —— 截取列

cut -d':' -f1 /etc/passwd          # 用户名
cut -d':' -f1,3 /etc/passwd        # 用户名 + UID
cut -c2 chinese.txt                # 每行第 2 个字符(中文用 -c)

按照冒号截取

截取第一列和第三列

截取1到4列

cut -b 截取英文字符


 

[root@zard3 shelldemo]# cut -b 3 test2.txt
e
 
e
e
o
e
=
w
t


o
o
A
b
s
cut -c 截取中文字符

文本如下

[root@zard3 shelldemo]# cut -c 2 test3.txt 
诚
雨
程
祥
瀚
潮

但是,cut -b不适合中文

[root@zard3 shelldemo]# cut -b 2 test3.txt 
 
 
 
­
 
 # 运行结果啥也没有

⚠️ cut 只能处理「单字符分隔」的文本。

2.2 sort —— 排序

sort file                          # 按字典序
sort -t: -k3 -n /etc/passwd        # 以冒号分隔,按第 3 列数值升序
sort -nr -k5 -o result.log log     # 降序并落盘
sort 按字符排序

sort -n 按照数字大小升序

sort -nr 降序排列

sort -u去重

运行
[root@zard3 shelldemo]# sort -u test4.txt 
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
test3:x:1004:1004::/home/test3:/bin/bash
test4:x:1005:1005::/home/test4:/bin/bash
zard111:x:1003:1003::/home/zard111:/bin/bash

2.3 uniq —— 去重计数

uniq 只能去相邻重复 → 先 sort 再 uniq 成了铁律。

sort fruit.txt | uniq -c          # 每行出现次数
sort fruit.txt | uniq -d          # 只显示重复行
who | awk '{print $1}' | sort | uniq   # 当前在线用户
uniq 仅仅去重复
[root@zard3 shelldemo]# uniq test5.txt 
pple
apple
peache
pear
banana
cherry
banana
orange

全局去重

[root@zard3 shelldemo]# sort test5.txt |uniq
apple
banana
cherry
orange
peache
pear
uniq -c 统计重复的次数

文本

[root@zard3 shelldemo]# cat test5.txt 
apple
apple
peache
pear
banana
cherry
cherry
banana
orange

命令

# 全局去重,并统计每行出现的次数
[root@zard3 shelldemo]# sort test5.txt |uniq -c
      2 apple
      2 banana
      2 cherry
      1 orange
      1 peache
      1 pear
uniq -d 只显示重复行
[root@zard3 shelldemo]# sort test5.txt |uniq -d
apple
banana
cherry
uniq -u 显示不重复的行
[root@zard3 shelldemo]# cat test5.txt |sort |uniq -u
orange
peache
pear

2.4 tr —— 字符级替换/删除/压缩

bash

复制

tr 'a-z' 'A-Z' < fruit.txt        # 小写 → 大写
tr -d '\n' < file                 # 删除换行
tr -s ' '                         # 把连续空格压成一个

文本 同上

替换是一一对应的字母替换

[root@zard3 shelldemo]# cat test5.txt |tr "a-z" "A-Z"
APPLE
APPLE
PEACHE
PEAR
BANANA
CHERRY
CHERRY
BANANA
ORANGE
a替换成s,p替换成a,le替换成r
[root@zard3 shelldemo]# cat test5.txt |tr "apple" "star"
saarr
saarr
arschr
arsr
bsnsns
chrrry
chrrry
bsnsns
orsngr

三、sed 流编辑器

3.1 工作模型(背下来)

  1. 读一行 → 2. 模式空间 → 3. 执行脚本 → 4. 输出 → 5. 清空 → 循环

默认不改原文件,加 -i 才真正落盘。

3.2 高频指令模板

任务 模板 例子
删行 sed '/pattern/d' sed '/^$/d' 删空行
替换 sed 's/old/new/g' sed -i.bak 's/SELINUX=disabled/enable/' /etc/selinux/config
打印 sed -n '3,5p' 输出 3-5 行
迁移 sed '/pat/{H;d};$G' 含 pat 的行移到末尾
脚本 sed -f script.sed file 多条指令复用
[root@zard3 shelldemo]# cat passwd.txt 
     1	root:x:0:0:root:/root:/bin/bash
     2	bin:x:1:1:bin:/bin:/sbin/nologin
     3	daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4	adm:x:3:4:adm:/var/adm:/sbin/nologin
     5	lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     6	sync:x:5:0:sync:/sbin:/bin/sync
     7	shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     8	halt:x:7:0:halt:/sbin:/sbin/halt
     9	mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10	operator:x:11:0:operator:/root:/sbin/nologin
    11	games:x:12:100:games:/usr/games:/sbin/nologin
    12	ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    13	nobody:x:99:99:Nobody:/:/sbin/nologin
    14	systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    15	dbus:x:81:81:System message bus:/:/sbin/nologin
    16	polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    17	libstoragemgmt:x:998:995:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
    18	colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin
    19	rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
    20	saned:x:996:993:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
    21	gluster:x:995:992:GlusterFS daemons:/run/gluster:/sbin/nologin
    22	saslauth:x:994:76:Saslauthd user:/run/saslauthd:/sbin/nologin
    23	abrt:x:173:173::/etc/abrt:/sbin/nologin
    24	setroubleshoot:x:993:990::/var/lib/setroubleshoot:/sbin/nologin
    25	rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
    26	pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
    27	radvd:x:75:75:radvd user:/:/sbin/nologin
    28	chrony:x:992:987::/var/lib/chrony:/sbin/nologin
    29	unbound:x:991:986:Unbound DNS resolver:/etc/unbound:/sbin/nologin
    30	qemu:x:107:107:qemu user:/:/sbin/nologin
    31	tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
    32	sssd:x:990:984:User for sssd:/:/sbin/nologin
    33	usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
    34	geoclue:x:989:983:User for geoclue:/var/lib/geoclue:/sbin/nologin
    35	ntp:x:38:38::/etc/ntp:/sbin/nologin
    36	gdm:x:42:42::/var/lib/gdm:/sbin/nologin
    37	rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
    38	nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
    39	gnome-initial-setup:x:988:982::/run/gnome-initial-setup/:/sbin/nologin
    40	sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    41	avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
    42	postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    43	tcpdump:x:72:72::/:/sbin/nologin
    44	zard3:x:1000:1000:ZARD3:/home/zard3:/bin/bash
    45	test1:x:1001:1001::/home/test1:/bin/bash
    46	tom:x:1002:1002::/home/tom:/bin/bash
    47	mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/false
    48	named:x:25:25:Named:/var/named:/sbin/nologin
    49	apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
    50	zard111:x:1003:1003::/home/zard111:/bin/bash
    51	test3:x:1004:1004::/home/test3:/bin/bash
    52	test4:x:1005:1005::/home/test4:/bin/bash
sed -n "p" passwd.txt 输出的结果和cat一样
[root@zard3 shelldemo]# sed -n "p" passwd.txt 
     1	root:x:0:0:root:/root:/bin/bash
     2	bin:x:1:1:bin:/bin:/sbin/nologin
     3	daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4	adm:x:3:4:adm:/var/adm:/sbin/nologin
     5	lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     6	sync:x:5:0:sync:/sbin:/bin/sync
     7	shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     8	halt:x:7:0:halt:/sbin:/sbin/halt
     9	mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10	operator:x:11:0:operator:/root:/sbin/nologin
    11	games:x:12:100:games:/usr/games:/sbin/nologin
    12	ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    13	nobody:x:99:99:Nobody:/:/sbin/nologin
    14	systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    15	dbus:x:81:81:System message bus:/:/sbin/nologin
    16	polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    17	libstoragemgmt:x:998:995:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
    18	colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin
    19	rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
    20	saned:x:996:993:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
    21	gluster:x:995:992:GlusterFS daemons:/run/gluster:/sbin/nologin
    22	saslauth:x:994:76:Saslauthd user:/run/saslauthd:/sbin/nologin
    23	abrt:x:173:173::/etc/abrt:/sbin/nologin
    24	setroubleshoot:x:993:990::/var/lib/setroubleshoot:/sbin/nologin
    25	rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
    26	pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
    27	radvd:x:75:75:radvd user:/:/sbin/nologin
    28	chrony:x:992:987::/var/lib/chrony:/sbin/nologin
    29	unbound:x:991:986:Unbound DNS resolver:/etc/unbound:/sbin/nologin
    30	qemu:x:107:107:qemu user:/:/sbin/nologin
    31	tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
    32	sssd:x:990:984:User for sssd:/:/sbin/nologin
    33	usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
    34	geoclue:x:989:983:User for geoclue:/var/lib/geoclue:/sbin/nologin
    35	ntp:x:38:38::/etc/ntp:/sbin/nologin
    36	gdm:x:42:42::/var/lib/gdm:/sbin/nologin
    37	rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
    38	nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
    39	gnome-initial-setup:x:988:982::/run/gnome-initial-setup/:/sbin/nologin
    40	sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    41	avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
    42	postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    43	tcpdump:x:72:72::/:/sbin/nologin
    44	zard3:x:1000:1000:ZARD3:/home/zard3:/bin/bash
    45	test1:x:1001:1001::/home/test1:/bin/bash
    46	tom:x:1002:1002::/home/tom:/bin/bash
    47	mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/false
    48	named:x:25:25:Named:/var/named:/sbin/nologin
    49	apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
    50	zard111:x:1003:1003::/home/zard111:/bin/bash
    51	test3:x:1004:1004::/home/test3:/bin/bash
    52	test4:x:1005:1005::/home/test4:/bin/bash
sed -n "10p" passwd.txt 输入第十行
[root@zard3 shelldemo]# sed -n "10p" passwd.txt 
    10	operator:x:11:0:operator:/root:/sbin/nologin
sed -n "4,6p" passwd.txt 输入4-6行
[root@zard3 shelldemo]# sed -n "4,6p" passwd.txt 
     4	adm:x:3:4:adm:/var/adm:/sbin/nologin
     5	lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     6	sync:x:5:0:sync:/sbin:/bin/sync
sed -n "p;n" passwd.txt  输入所有的奇数行
[root@zard3 shelldemo]# sed -n "p;n" passwd.txt 
     1	root:x:0:0:root:/root:/bin/bash
     3	daemon:x:2:2:daemon:/sbin:/sbin/nologin
     5	lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     7	shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     9	mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    11	games:x:12:100:games:/usr/games:/sbin/nologin
    13	nobody:x:99:99:Nobody:/:/sbin/nologin
    15	dbus:x:81:81:System message bus:/:/sbin/nologin
    17	libstoragemgmt:x:998:995:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
    19	rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
    21	gluster:x:995:992:GlusterFS daemons:/run/gluster:/sbin/nologin
    23	abrt:x:173:173::/etc/abrt:/sbin/nologin
    25	rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
    27	radvd:x:75:75:radvd user:/:/sbin/nologin
    29	unbound:x:991:986:Unbound DNS resolver:/etc/unbound:/sbin/nologin
    31	tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
    33	usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
    35	ntp:x:38:38::/etc/ntp:/sbin/nologin
    37	rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
    39	gnome-initial-setup:x:988:982::/run/gnome-initial-setup/:/sbin/nologin
    41	avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
    43	tcpdump:x:72:72::/:/sbin/nologin
    45	test1:x:1001:1001::/home/test1:/bin/bash
    47	mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/false
    49	apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
    51	test3:x:1004:1004::/home/test3:/bin/bash
sed -n "n;p" passwd.txt 显示所有的偶数行
[root@zard3 shelldemo]# sed -n "n;p" passwd.txt 
     2	bin:x:1:1:bin:/bin:/sbin/nologin
     4	adm:x:3:4:adm:/var/adm:/sbin/nologin
     6	sync:x:5:0:sync:/sbin:/bin/sync
     8	halt:x:7:0:halt:/sbin:/sbin/halt
    10	operator:x:11:0:operator:/root:/sbin/nologin
    12	ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    14	systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    16	polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    18	colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin
    20	saned:x:996:993:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
    22	saslauth:x:994:76:Saslauthd user:/run/saslauthd:/sbin/nologin
    24	setroubleshoot:x:993:990::/var/lib/setroubleshoot:/sbin/nologin
    26	pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
    28	chrony:x:992:987::/var/lib/chrony:/sbin/nologin
    30	qemu:x:107:107:qemu user:/:/sbin/nologin
    32	sssd:x:990:984:User for sssd:/:/sbin/nologin
    34	geoclue:x:989:983:User for geoclue:/var/lib/geoclue:/sbin/nologin
    36	gdm:x:42:42::/var/lib/gdm:/sbin/nologin
    38	nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
    40	sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    42	postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    44	zard3:x:1000:1000:ZARD3:/home/zard3:/bin/bash
    46	tom:x:1002:1002::/home/tom:/bin/bash
    48	named:x:25:25:Named:/var/named:/sbin/nologin
    50	zard111:x:1003:1003::/home/zard111:/bin/bash
    52	test4:x:1005:1005::/home/test4:/bin/bash
sed -n "1,12{p;n}" passwd.txt  输出1-12行所有的奇数行
[root@zard3 shelldemo]# sed -n "1,12{p;n}" passwd.txt 
     1	root:x:0:0:root:/root:/bin/bash
     3	daemon:x:2:2:daemon:/sbin:/sbin/nologin
     5	lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     7	shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     9	mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    11	games:x:12:100:games:/usr/games:/sbin/nologin
sed -n "1,12{n;p}" passwd.txt 输出1-12行所有的偶数行
[root@zard3 shelldemo]# sed -n "1,12{n;p}" passwd.txt 
     2	bin:x:1:1:bin:/bin:/sbin/nologin
     4	adm:x:3:4:adm:/var/adm:/sbin/nologin
     6	sync:x:5:0:sync:/sbin:/bin/sync
     8	halt:x:7:0:halt:/sbin:/sbin/halt
    10	operator:x:11:0:operator:/root:/sbin/nologin
    12	ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
sed -n '45,${p}' passwd.txt  打印45行之后的所有行
[root@zard3 shelldemo]# sed -n '45,${p}' passwd.txt 
    45	test1:x:1001:1001::/home/test1:/bin/bash
    46	tom:x:1002:1002::/home/tom:/bin/bash
    47	mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/false
    48	named:x:25:25:Named:/var/named:/sbin/nologin
    49	apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
    50	zard111:x:1003:1003::/home/zard111:/bin/bash
    51	test3:x:1004:1004::/home/test3:/bin/bash
    52	test4:x:1005:1005::/home/test4:/bin/bash
sed -n '/the/p' test2.txt  带有the的每一行
[root@zard3 shelldemo]# sed -n '/the/p' test2.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.
sed -n '4,/the/p' test2.txt 以第四行作为起点,到第一次出现the的行
[root@zard3 shelldemo]# sed -n '4,/the/p' test2.txt 
     4	the tongue is boneless but it breaks bones.12! 
     5	google is the best tools for search keyword.
sed -n '/the/=' test2.txt 出现the的行号
[root@zard3 shelldemo]# sed -n '/the/=' test2.txt 
4
5
6
sed -n '/^PI/p' test1.txt 找到开头为PI的行
[root@zard3 shelldemo]# sed -n '/^PI/p' test1.txt 
PI=3.141592653589793238462643383249901429
nl test1.txt|sed '6d' 删除第六行
[root@zard3 shelldemo]# nl test1.txt|sed '6d'
     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.
     7	PI=3.141592653589793238462643383249901429
     8	a wood cross!
     9	Actions speak louder than words
       
       
    10	#woood #
    11	#woooooood 
    12	# AxyzxyzxyzxyzC
    13	I bet this place is really spooky late at night! 
    14	Misfortunes never come alone/single.
sed '/^[a-z]/d' test1.txt 删除所有的以小写字母开头的行
[root@zard3 shelldemo]# sed '/^[a-z]/d' test1.txt 
He was wearing a blue polo shirt with black pants. 
The home of Football on BBC Sport online.
The year ahead will test our political establishment to the limit.
PI=3.141592653589793238462643383249901429
Actions speak louder than words


#woood #
#woooooood 
# AxyzxyzxyzxyzC
I bet this place is really spooky late at night! 
Misfortunes never come alone/single.
sed '/\.$/d' test2.txt 删除所有以.结尾的行
[root@zard3 shelldemo]# sed '/\.$/d' test2.txt 
     2	He was wearing a blue polo shirt with black pants. 
     4	the tongue is boneless but it breaks bones.12! 
     7	PI=3.141592653589793238462643383249901429
     8	a wood cross!
     9	Actions speak louder than words
    10	
    11	
    12	#woood #
    13	#woooooood 
    14	# AxyzxyzxyzxyzC
    15	I bet this place is really spooky late at night! 
sed '/^$/d' test1.txt 删除所有的空行
[root@zard3 shelldemo]# sed '/^$/d' test1.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.
sed '/the/{H;d}; $G' test1.txt 将包含the 的行迁移至文件末尾,{;}用于多个操作

sed '/the/w the.file' test2.txt  将包含the的行转移到the.file

sed -i.bak 's/SELINUX=permissive/SELINUX=enforcing/' /etc/selinux/config 替换

 sed '8a+句子' test2.txt 在第八行下面加一句话

 sed '/the/a+句子' test2.txt 在含有the的句子下面加一句话

3.3 生产案例

  1. 一键修改网卡 IP        

    sed -i 's/^IPADDR=.*/IPADDR=192.168.10.100/' /etc/sysconfig/network-scripts/ifcfg-ens33
  2. Apache 改监听地址 + 端口

    bash

    复制

    sed -i 's/^Listen .*/Listen 192.168.10.100:8080/' /etc/httpd/conf/httpd.conf
  3. vsftpd 安全加固脚本(禁止匿名、允许本地)

    bash

    复制

    #!/bin/bash
    CONFIG=/etc/vsftpd/vsftpd.conf
    [ ! -f $CONFIG.bak ] && cp $CONFIG $CONFIG.bak
    sed -e '/^anonymous_enable/s/YES/NO/' \
        -e '/^local_enable/s/NO/YES/' \
        -e '/^write_enable/s/NO/YES/' $CONFIG > tmp
    mv tmp $CONFIG
    systemctl restart vsftpd

总结

        经过两套速查资料的梳理,我们已经把 Linux 文本处理的核心武器握在手中:cut 精准截取列,sort 与 uniq 联手完成排序与去重,tr 在字符级别做替换、删除与压缩,而 sed 则凭借“模式空间”机制,提供行级乃至跨行级的增删改查、迁移与就地编辑能力。
        它们的组合套路可以归纳为一句话——先用 cut/sort/uniq/tr 完成“数据清洗”,再用 sed 做“结构重塑”。面对日志、配置、监控输出或任何纯文本场景,只要记住这条流水线,就能在 1~3 行命令内完成传统脚本几十行才能实现的任务。
        下一步建议把示例亲手敲一遍,将命令固化为肌肉记忆,并在生产环境中大胆实践:在线扩容配置文件、批量替换 IP、实时统计连接数……你会发现,所谓高效运维,不过是把文本玩到极致。