Linux 文本处理三大利器:命令小工具和sed
目录
前言
一、命令小工具:简单高效的文本处理利器
1. cut —— 按列或字符截取
2. sort —— 排序工具
3. uniq —— 去除连续重复行
4. tr —— 字符替换 / 删除 / 压缩重复
二、sed:强大的流编辑器
1. sed 是什么
2. sed 的工作原理
3. sed 命令常见用法
4. 常用示例
三. 总结
前言
在 Linux 系统中,文本处理是日常工作中不可或缺的一部分。无论是日志分析、数据提取还是还是配置文件修改,都需要高效的文本处理工具。本文将依次介绍常用的命令小工具、sed 流编辑器,帮助你快速掌握 Linux 文本处理的精髓。
一、命令小工具:简单高效的文本处理利器
1. cut —— 按列或字符截取
cut
命令常用于从文本中抽取需要的字段,特别适合处理结构化的文本数据。
常用选项:
-
-b
:按字节截取 -
-c
:按字符截取(处理中文推荐使用) -
-d
:指定分隔符(默认是 TAB 键) -
-f
:指定字段(需配合-d
使用)
示例:
# 截取/etc/passwd文件中的第1列(用户名)
cut -d':' -f1 /etc/passwd# 截取第3列(UID)
cut -d':' -f3 /etc/passwd# 同时截取第1和3列
cut -d':' -f1,3 /etc/passwd# 截取name.txt文件中每行的第2个字符
cut -c 2 name.txt注意:cut 只擅长处理以单个字符为间隔的文本。
[root@110 function]# cat fun10.sh #原脚本
#!/bin/bash
lisa3:x:707:1007::/home/lisa3:/bin/bash
webmaster:x:1005:10::/home/admin:/bin/bash
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/false
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
named:x:25:25:Named:/var/named:/sbin/nologin
zc:x:1006:1006::/home/zc:/bin/bash
zc2:x:1007:1008::/home/zc2:/bin/bash
yang:x:1008:1009::/home/yang:/bin/bash
vivi:x:1009:1010::/home/vivi:/bin/bash
pi:x:1010:1011::/home/pi:/bin/bash[root@110 function]# cut -d':' -f1 fun10.sh #截取第一列(用户名)
#!/bin/bash
lisa3
webmaster
mysql
apache
named
zc
zc2
yang
vivi
pi[root@110 function]# cut -d':' -f3 fun10.sh #截取第三行(UID)
#!/bin/bash
707
1005
27
48
25
1006
1007
1008
1009
1010[root@110 function]# cut -d':' -f1,3 fun10.sh #截取1、3行
#!/bin/bash
lisa3:707
webmaster:1005
mysql:27
apache:48
named:25
zc:1006
zc2:1007
yang:1008
vivi:1009
pi:1010[root@110 function]# cut -c 2 fun10.sh #截取第二个字符
!
i
e
y
p
a
c
c
a
i
i[root@110 function]# who|cut -b 3 #截取第三个字符
o
o
o
[root@110 function]# who|cut -c 2 #截取第二个字符(适合中文)
o
o
o
2. sort —— 排序工具
sort
命令用于对文本内容进行排序,默认按行首字符升序排列。
常用选项:
-
-t
:指定分隔符 -
-k
:指定排序字段 -
-n
:按数值排序(默认是字典序) -
-r
:降序排列 -
-u
:去重(功能等同于uniq
) -
-o
:将排序结果输出到文件
示例:
# 按第一列升序排序passwd.txt
sort passwd.txt# 以冒号为分隔符,按第3列数值升序排序
sort -n -t: -k3 passwd.txt# 第3列数值降序排序
sort -nr -t: -k3 passwd.txt# 去重并排序
sort -u passwd.txt# 将排序结果保存到out.txt
sort -nr -t: -k3 passwd.txt -o out.txt
[root@110 function]# cat fun10.sh #原脚本
#!/bin/bash
lisa3:x:707:1007::/home/lisa3:/bin/bash
webmaster:x:1005:10::/home/admin:/bin/bash
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/false
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
named:x:25:25:Named:/var/named:/sbin/nologin
zc:x:1006:1006::/home/zc:/bin/bash
zc2:x:1007:1008::/home/zc2:/bin/bash
yang:x:1008:1009::/home/yang:/bin/bash
vivi:x:1009:1010::/home/vivi:/bin/bash
pi:x:1010:1011::/home/pi:/bin/bash
zc2:x:1007:1008::/home/zc2:/bin/bash
yang:x:1008:1009::/home/yang:/bin/bash
vivi:x:1009:1010::/home/vivi:/bin/bash
pi:x:1010:1011::/home/pi:/bin/bash[root@110 function]# sort fun10.sh #按第一个列升序apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
#!/bin/bash
lisa3:x:707:1007::/home/lisa3:/bin/bash
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/false
named:x:25:25:Named:/var/named:/sbin/nologin
pi:x:1010:1011::/home/pi:/bin/bash
pi:x:1010:1011::/home/pi:/bin/bash
vivi:x:1009:1010::/home/vivi:/bin/bash
vivi:x:1009:1010::/home/vivi:/bin/bash
webmaster:x:1005:10::/home/admin:/bin/bash
yang:x:1008:1009::/home/yang:/bin/bash
yang:x:1008:1009::/home/yang:/bin/bash
zc2:x:1007:1008::/home/zc2:/bin/bash
zc2:x:1007:1008::/home/zc2:/bin/bash
zc:x:1006:1006::/home/zc:/bin/bash[root@110 function]# sort -n -t: -k3 fun10.sh #以冒号分隔,按第三列数值升序#!/bin/bash
named:x:25:25:Named:/var/named:/sbin/nologin
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/false
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
lisa3:x:707:1007::/home/lisa3:/bin/bash
webmaster:x:1005:10::/home/admin:/bin/bash
zc:x:1006:1006::/home/zc:/bin/bash
zc2:x:1007:1008::/home/zc2:/bin/bash
zc2:x:1007:1008::/home/zc2:/bin/bash
yang:x:1008:1009::/home/yang:/bin/bash
yang:x:1008:1009::/home/yang:/bin/bash
vivi:x:1009:1010::/home/vivi:/bin/bash
vivi:x:1009:1010::/home/vivi:/bin/bash
pi:x:1010:1011::/home/pi:/bin/bash
pi:x:1010:1011::/home/pi:/bin/bash[root@110 function]# sort -nr -t: -k3 fun10.sh #第三列数值升序
pi:x:1010:1011::/home/pi:/bin/bash
pi:x:1010:1011::/home/pi:/bin/bash
vivi:x:1009:1010::/home/vivi:/bin/bash
vivi:x:1009:1010::/home/vivi:/bin/bash
yang:x:1008:1009::/home/yang:/bin/bash
yang:x:1008:1009::/home/yang:/bin/bash
zc2:x:1007:1008::/home/zc2:/bin/bash
zc2:x:1007:1008::/home/zc2:/bin/bash
zc:x:1006:1006::/home/zc:/bin/bash
webmaster:x:1005:10::/home/admin:/bin/bash
lisa3:x:707:1007::/home/lisa3:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/false
named:x:25:25:Named:/var/named:/sbin/nologin
#!/bin/bash[root@110 function]# sort -u fun10.sh #去重apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
#!/bin/bash
lisa3:x:707:1007::/home/lisa3:/bin/bash
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/false
named:x:25:25:Named:/var/named:/sbin/nologin
pi:x:1010:1011::/home/pi:/bin/bash
vivi:x:1009:1010::/home/vivi:/bin/bash
webmaster:x:1005:10::/home/admin:/bin/bash
yang:x:1008:1009::/home/yang:/bin/bash
zc2:x:1007:1008::/home/zc2:/bin/bash
zc:x:1006:1006::/home/zc:/bin/bash[root@110 function]# sort -nr -t: -k3 fun10.sh -o 1.txt #排序结果保存
[root@110 function]# ls
1.txt fun02.sh fun04.sh fun06.sh fun08.sh fun10.sh
fun01.sh fun03.sh fun05.sh fun07.sh fun09.sh the.txt[root@110 function]# cat 1.txt #保存结果
pi:x:1010:1011::/home/pi:/bin/bash
pi:x:1010:1011::/home/pi:/bin/bash
vivi:x:1009:1010::/home/vivi:/bin/bash
vivi:x:1009:1010::/home/vivi:/bin/bash
yang:x:1008:1009::/home/yang:/bin/bash
yang:x:1008:1009::/home/yang:/bin/bash
zc2:x:1007:1008::/home/zc2:/bin/bash
zc2:x:1007:1008::/home/zc2:/bin/bash
zc:x:1006:1006::/home/zc:/bin/bash
webmaster:x:1005:10::/home/admin:/bin/bash
lisa3:x:707:1007::/home/lisa3:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/false
named:x:25:25:Named:/var/named:/sbin/nologin
#!/bin/bash
3. uniq —— 去除连续重复行
uniq
命令用于去除连续的重复行,注意:只能去掉相邻的重复行,所以通常先使用sort
排序后再用uniq
去重。
常用选项:
-
-c
:对重复的行进行计数 -
-d
:只显示重复行 -
-u
:只显示唯一行
示例:
# 创建示例文件
cat fruit.txt
apple
apple
peache
pear
banana
cherry
cherry
banana
orange# 去掉相邻重复行
uniq fruit.txt# 全局去重(先排序使重复行相邻)
sort fruit.txt | uniq# 统计每行出现次数
sort fruit.txt | uniq -c# 只显示重复行
sort fruit.txt | uniq -d# 只显示不重复行
sort fruit.txt | uniq -u
[root@110 function]# cat fun10.sh #原脚本
#!/bin/bash
lisa3:x:707:1007::/home/lisa3:/bin/bash
webmaster:x:1005:10::/home/admin:/bin/bash
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/false
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
named:x:25:25:Named:/var/named:/sbin/nologin
zc:x:1006:1006::/home/zc:/bin/bash
zc2:x:1007:1008::/home/zc2:/bin/bash
yang:x:1008:1009::/home/yang:/bin/bash
vivi:x:1009:1010::/home/vivi:/bin/bash
pi:x:1010:1011::/home/pi:/bin/bash
zc2:x:1007:1008::/home/zc2:/bin/bash
yang:x:1008:1009::/home/yang:/bin/bash
vivi:x:1009:1010::/home/vivi:/bin/bash
pi:x:1010:1011::/home/pi:/bin/bash[root@110 function]# sort fun10.sh|uniq #全局去重apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
#!/bin/bash
lisa3:x:707:1007::/home/lisa3:/bin/bash
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/false
named:x:25:25:Named:/var/named:/sbin/nologin
pi:x:1010:1011::/home/pi:/bin/bash
vivi:x:1009:1010::/home/vivi:/bin/bash
webmaster:x:1005:10::/home/admin:/bin/bash
yang:x:1008:1009::/home/yang:/bin/bash
zc2:x:1007:1008::/home/zc2:/bin/bash
zc:x:1006:1006::/home/zc:/bin/bash[root@110 function]# sort fun10.sh|uniq -c #统计每行每行次数1 1 apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin1 #!/bin/bash1 lisa3:x:707:1007::/home/lisa3:/bin/bash1 mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/false1 named:x:25:25:Named:/var/named:/sbin/nologin2 pi:x:1010:1011::/home/pi:/bin/bash2 vivi:x:1009:1010::/home/vivi:/bin/bash1 webmaster:x:1005:10::/home/admin:/bin/bash2 yang:x:1008:1009::/home/yang:/bin/bash2 zc2:x:1007:1008::/home/zc2:/bin/bash1 zc:x:1006:1006::/home/zc:/bin/bash[root@110 function]# sort fun10.sh|uniq -d #只显示重复行
pi:x:1010:1011::/home/pi:/bin/bash
vivi:x:1009:1010::/home/vivi:/bin/bash
yang:x:1008:1009::/home/yang:/bin/bash
zc2:x:1007:1008::/home/zc2:/bin/bash[root@110 function]# sort fun10.sh|uniq -u #只显示重复行apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
#!/bin/bash
lisa3:x:707:1007::/home/lisa3:/bin/bash
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/false
named:x:25:25:Named:/var/named:/sbin/nologin
webmaster:x:1005:10::/home/admin:/bin/bash
zc:x:1006:1006::/home/zc:/bin/bash[root@110 function]# who | awk '{print $1}'|uniq #查看登录用户
root[root@110 function]# last | awk '{print $1}'|sort|uniq|grep -v "^$" |grep -v wtmp #查看登录过系统的用户
reboot
root
zheng
4. tr —— 字符替换 / 删除 / 压缩重复
tr
命令主要用于单个字符的处理,不适合字段级别的操作。
常用选项:
-
-d
:删除指定字符 -
-s
:压缩重复字符,只保留一个
示例:
# 小写转大写
tr 'a-z' 'A-Z' < fruit.txt# 字符替换(一一对应)
cat fruit | tr 'apple' 'star' # a→s, p→a, l→r, e→r# 删除所有字母a
tr -d 'a' < fruit.txt# 删除换行符
tr -d '\n' < fruit.txt# 连续的p压缩成一个
tr -s 'p' < fruit.txt
[root@110 function]# cat fun09.sh #原脚本
#!/bin/bash
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.[root@110 function]# tr 'a-z' 'A-Z' < fun09.sh #小写转大写
#!/BIN/BASH
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.[root@110 function]# cat fun09.sh|tr 'a-z' 'A-Z' #小写转大写
#!/BIN/BASH
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.[root@110 function]# cat fun09.sh|tr 'the' 'THE' #一对一字母的替换
#!/bin/basH
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.[root@110 function]# cat fun09.sh|tr 'a' ' ' #把a替换成空格
#!/bin/b sh
he w s short nd f t.
He w s we ring blue polo shirt with bl ck p nts.
The home of Footb ll on BBC Sport online.
the tongue is boneless but it bre ks bones.12!
google is the best tools for se rch keyword.
The ye r he d will test our politic l est blishment to the limit.
PI=3.141592653589793238462643383249901429wood cross!
Actions spe k louder th n words#woood #
#woooooood
# AxyzxyzxyzxyzC
I bet this pl ce is re lly spooky l te t night!
Misfortunes never come lone/single.[root@110 function]# tr 'a' '/' < fun09.sh #把a替换成/
#!/bin/b/sh
he w/s short /nd f/t.
He w/s we/ring / blue polo shirt with bl/ck p/nts.
The home of Footb/ll on BBC Sport online.
the tongue is boneless but it bre/ks bones.12!
google is the best tools for se/rch keyword.
The ye/r /he/d will test our politic/l est/blishment to the limit.
PI=3.141592653589793238462643383249901429
/ wood cross!
Actions spe/k louder th/n words#woood #
#woooooood
# AxyzxyzxyzxyzC
I bet this pl/ce is re/lly spooky l/te /t night!
Misfortunes never come /lone/single.[root@110 function]# tr -d 'a' < fun09.sh #删除所有的a
#!/bin/bsh
he ws short nd ft.
He ws wering blue polo shirt with blck pnts.
The home of Footbll on BBC Sport online.
the tongue is boneless but it breks bones.12!
google is the best tools for serch keyword.
The yer hed will test our politicl estblishment to the limit.
PI=3.141592653589793238462643383249901429wood cross!
Actions spek louder thn words#woood #
#woooooood
# AxyzxyzxyzxyzC
I bet this plce is relly spooky lte t night!
Misfortunes never come lone/single.[root@110 function]# tr -d '\n' < fun09.sh #删除换行符
#!/bin/bashhe 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.141592653589793238462643383249901429a wood cross!Actions speak louder than words#woood ##woooooood # AxyzxyzxyzxyzCI bet this place is really spooky late at night! Misfortunes never come alone/single.[root@110 function]# [root@110 function]# tr -s 'p' < fun09.sh #连续p压缩成一个
#!/bin/bash
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:强大的流编辑器
1. sed 是什么
sed(Stream Editor)是一种流编辑器,它可以依照脚本的指令来处理、编辑文本文件。主要用于自动编辑一个或多个文件、简化对文件的反复操作、编写转换程序等。
2. sed 的工作原理
sed 的工作流程主要包括读取、执行和显示三个过程:
-
读取:从输入流(文件、管道、标准输入)中读取一行内容并存储到临时缓冲区(模式空间,pattern space)。
-
执行:默认情况下,所有 sed 命令都在模式空间中顺序执行,除非指定了行地址,否则会在所有行上依次执行。
-
显示:将修改后的内容发送到输出流,之后模式空间被清空。
上述过程会重复执行,直到所有内容处理完毕。默认情况下,sed 不会修改原文件,除非使用重定向存储输出。
3. sed 命令常见用法
基本语法:
sed [选项] '操作' 参数
sed [选项] -f scriptfile 参数
常用选项:
-
-e
:用指定命令处理输入文本 -
-f
:用指定的脚本文件处理输入文本 -
-n
:仅显示处理后的结果 -
-i
:直接编辑原文件(建议先备份,如-i.bak
) -
-r
:使用扩展正则表达
常用操作:
-
a
:在当前行下面增加一行指定内容 -
c
:替换选定行 -
d
:删除选定行 -
i
:在选定行上面插入一行指定内容 -
p
:打印指定行 -
s
:替换指定字符 -
y
:字符转换
4. 常用示例
① 输出符合条件的文本:
# 输出所有内容(等同于cat demo)
sed -n 'p' demo# 输出第3行
sed -n '3p' demo# 输出3~5行
sed -n '3,5p' demo# 输出所有奇数行
sed -n 'p;n' demo# 输出包含"the"的行
sed -n '/the/p' demo# 输出以数字结尾的行
sed -n '/[0-9]$/p' demo
[root@110 function]# sed -n '3p' fun09.sh #输出第三行
He was wearing a blue polo shirt with black pants. [root@110 function]# sed -n '3,5p' fun09.sh #输出第三到第五行
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! [root@110 function]# sed -n 'p;n' fun09.sh #输出所有奇数行
#!/bin/bash
He was wearing a blue polo shirt with black pants.
the tongue is boneless but it breaks bones.12!
The year ahead will test our political establishment to the limit.
a wood cross!#woood #
# AxyzxyzxyzxyzC
Misfortunes never come alone/single.[root@110 function]# sed -n 'n;p' fun09.sh #输出所有偶数行
he was short and fat.
The home of Football on BBC Sport online.
google is the best tools for search keyword.
PI=3.141592653589793238462643383249901429
Actions speak louder than words#woooooood
I bet this place is really spooky late at night! [root@110 function]# sed -n '1,5{p;n}' fun09.sh #输出1-5行的奇数行
#!/bin/bash
He was wearing a blue polo shirt with black pants.
the tongue is boneless but it breaks bones.12! [root@110 function]# sed -n '10,${n;p}' fun09.sh #输出第十行至行尾的偶数行#woood #
# AxyzxyzxyzxyzC
Misfortunes never come alone/single.
以此脚本为原本:
输出命令:
[root@110 function]# sed -n '4,/the/p' fun09.sh #输出从第四行至第一个包含the的行
The home of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12! [root@110 function]# sed -n '5,/the/p' fun09.sh #输出从第五行至第一个包含the的行
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword.[root@110 function]# sed -n '/the/=' fun09.sh #输出包含the的行的行号
5
6
7[root@110 function]# sed -n "/a/p" fun09.sh #输出包含a的行
#!/bin/bash
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.
a wood cross!
Actions speak louder than words
I bet this place is really spooky late at night!
Misfortunes never come alone/single.[root@110 function]# sed -n "/^#/p" fun09.sh #输出以#开头的行
#!/bin/bash
#woood #
#woooooood
# AxyzxyzxyzxyzC[root@110 function]# sed -n "/[0-9]$/p" fun09.sh #输出以数字结尾的行
PI=3.141592653589793238462643383249901429[root@110 function]# sed -n "/\<wood\>/p" fun09.sh #输出包含wood单词的行
a wood cross!
② 删除符合条件的文本:
[root@110 function]# nl fun09.sh|sed '3d' #删除第三行1 #!/bin/bash2 he was short and fat.4 The home of Football on BBC Sport online.5 the tongue is boneless but it breaks bones.12! 6 google is the best tools for search keyword.7 The year ahead will test our political establishment to the limit.8 PI=3.1415926535897932384626433832499014299 a wood cross!10 Actions speak louder than words11 #woood #12 #woooooood 13 # AxyzxyzxyzxyzC14 I bet this place is really spooky late at night! 15 Misfortunes never come alone/single.[root@110 function]# nl fun09.sh|sed '3,5d' #删除三到五行1 #!/bin/bash2 he was short and fat.6 google is the best tools for search keyword.7 The year ahead will test our political establishment to the limit.8 PI=3.1415926535897932384626433832499014299 a wood cross!10 Actions speak louder than words11 #woood #12 #woooooood 13 # AxyzxyzxyzxyzC14 I bet this place is really spooky late at night! 15 Misfortunes never come alone/single.[root@110 function]# nl fun09.sh|sed '/cross/d' #删除所以包含cross的行1 #!/bin/bash2 he was short and fat.3 He was wearing a blue polo shirt with black pants. 4 The home of Football on BBC Sport online.5 the tongue is boneless but it breaks bones.12! 6 google is the best tools for search keyword.7 The year ahead will test our political establishment to the limit.8 PI=3.14159265358979323846264338324990142910 Actions speak louder than words11 #woood #12 #woooooood 13 # AxyzxyzxyzxyzC14 I bet this place is really spooky late at night! 15 Misfortunes never come alone/single.[root@110 function]# sed '/^[a-z]/d' fun09.sh #删除以小写字母开头的行
#!/bin/bash
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.[root@110 function]# nl fun09.sh|sed '/\.$/d' #删除以.结尾的行1 #!/bin/bash3 He was wearing a blue polo shirt with black pants. 5 the tongue is boneless but it breaks bones.12! 8 PI=3.1415926535897932384626433832499014299 a wood cross!10 Actions speak louder than words11 #woood #12 #woooooood 13 # AxyzxyzxyzxyzC14 I bet this place is really spooky late at night! [root@110 function]# sed '/^$/d' fun09.sh #删除所有的空行
#!/bin/bash
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.
③ 替换符合条件的文本:
# 将每行中第一个"the"替换为"THE"
sed 's/the/THE/' demo# 将每行中第2个"l"替换为"L"
sed 's/l/L/2' demo# 将文件中所有"the"替换为"THE"
sed 's/the/THE/g' demo# 在每行行首插入#号
sed 's/^/#/' demo# 在包含"the"的行首插入#号
sed '/the/s/^/#/' demo
[root@110 function]# sed 's/the/THE/' fun09.sh #把每行第一个the替换成THE
#!/bin/bash
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.[root@110 function]# sed 's/l/L/2' fun09.sh #把每行第二个l替换成L
#!/bin/bash
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.[root@110 function]# sed 's/the/THE/g' fun09.sh #把文件中所有的the替换成THE
#!/bin/bash
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.[root@110 function]# sed 's/o//g' fun09.sh #把文件中所有的o替换成O
#!/bin/bash
he was shrt and fat.
He was wearing a blue pl shirt with black pants.
The hme f Ftball n BBC Sprt nline.
the tngue is bneless but it breaks bnes.12!
ggle is the best tls fr search keywrd.
The year ahead will test ur plitical establishment t the limit.
PI=3.141592653589793238462643383249901429
a wd crss!
Actins speak luder than wrds#wd #
#wd
# AxyzxyzxyzxyzC
I bet this place is really spky late at night!
Misfrtunes never cme alne/single.[root@110 function]# sed 's/^/#/g' fun09.sh # 把文件中所有的行的行首插入#
##!/bin/bash
#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.[root@110 function]# sed 's/$/EOT/' fun09.sh #把每行行尾插入EOT
#!/bin/bashEOT
he was short and fat.EOT
He was wearing a blue polo shirt with black pants. EOT
The home of Football on BBC Sport online.EOT
the tongue is boneless but it breaks bones.12! EOT
google is the best tools for search keyword.EOT
The year ahead will test our political establishment to the limit.EOT
PI=3.141592653589793238462643383249901429EOT
a wood cross!EOT
Actions speak louder than wordsEOT
EOT
EOT
#woood #EOT
#woooooood EOT
# AxyzxyzxyzxyzCEOT
I bet this place is really spooky late at night! EOT
Misfortunes never come alone/single.EOT[root@110 function]# sed '3,5s/the/EOT/g' fun09.sh #将3-5行中所有the换成EOT
#!/bin/bash
he was short and fat.
He was wearing a blue polo shirt with black pants.
The home of Football on BBC Sport online.
EOT 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.[root@110 function]# sed '/the/s/o/O/g' fun09.sh #将包含the的所有行中的o都替换成O
#!/bin/bash
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.[root@110 function]# cat /etc/selinux/config # This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=enable
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted [root@110 function]# sed -i.bak 's/SELINUX=enabled/SELINUX=disabled/' /etc/selinux/config
[root@110 function]# cat /etc/selinux/config # This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
三. 总结
命令小工具:cut(剪裁列) sort(排序) uniq(去重计数) tr(替换 删除 压缩)sed:
sed(Stream Editor)是一种流编辑器,它可以依照脚本的指令来处理、编辑文本文件。
工作流程包括读取、执行和显示
常用选项:
-e:用指定命令处理输入文本
-f:用指定的脚本文件处理输入文本
-n:仅显示处理后的结果
-i:直接编辑原文件(建议先备份,如-i.bak)
-r:使用扩展正则表达
常用操作:
a:在当前行下面增加一行指定内容
c:替换选定行
d:删除选定行
i:在选定行上面插入一行指定内容
p:打印指定行
s:替换指定字符
y:字符转换