sed是用来解析和转换文本的工具,它使用简单,是简洁的程序设计语言。
sed编辑器
- (一) sed编辑器基础
- 1. 简介
- 2. sed的模式空间
- (二)基本的sed编辑命令
- (三)sed命令实例
- 1. 向文件中添加或插入行
- 2. 更改文件中的指定的行
- 3. 删除文件中的行
- 4. 替换文件中内容
- 5. 打印文件中的行
(一) sed编辑器基础
1. 简介
- 自动化地编辑一个或多个文件
- 简化在多个文件中执行相同编辑地任务
- 编写转换程序
2. sed的模式空间
sed维护一种模式空间,即一个工作区或临时缓冲区,当使用编辑命令时,将在那存储单个输入行。下图展示了进行模式空间转换的一个两行的sed脚本。它将“The linux system”转换成“The LINUX Operating System”
sed一次处理一行输入的优点是在读取非常庞大的文件时不会出现问题,一般的文本编辑器必须将整个文件读入内存,这将会产生内存溢出或者在处理庞大的文件时速度非常慢。
(二)基本的sed编辑命令
- a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
- c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
- d :删除,因为是删除啊,所以 d 后面通常不接任何东东;
- i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
- p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
- s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正则表达式!
(三)sed命令实例
1. 向文件中添加或插入行
实例1:在文件的指定行之后添加一行内容。
$ cat info.txt
Linux - Sysadmin
Databased - Oracle, MySQL etc
Security - Firewall, Network, Oline Security etc
Cool - Websities
Storage - NetApp, EMC etc
Productivity - Too many technologies to explore, no much time available
使用sed的追加编辑命令(a)在上述文件的第5行添加一行命令:
[ley@ley-web ~]$ sed '5a\
> Solaris - Sysadmin, Recovery etc.' info.txt
Linux - Sysadmin
Databased - Oracle, MySQL etc
Security - Firewall, Network, Oline Security etc
Cool - Websities
Storage - NetApp, EMC etc
Solaris - Sysadmin, Recovery etc.
Productivity - Too many technologies to explore, no much time available
实例2:在匹配模式的行之后添加一行内容
$ sed '/Databases/a\
> Solaris - Sysadmin, Recovery etc.' info.txt
Linux - Sysadmin
Databased - Oracle, MySQL etc
Security - Firewall, Network, Oline Security etc
Cool - Websities
Storage - NetApp, EMC etc
Productivity - Too many technologies to explore, no much time available
实例3:在文本的最后一行后添加多行内容
在info.txt文件后面添加两行
$ sed '$a\
> Solaris - Sysadmin, Recovery etc.\
> Windows - Sysadmin etc.' info.txt
Linux - Sysadmin
Databased - Oracle, MySQL etc
Security - Firewall, Network, Oline Security etc
Cool - Websities
Storage - NetApp, EMC etc
Productivity - Too many technologies to explore, no much time availableSolaris - Sysadmin, Recovery etc.
Windows - Sysadmin etc.
实例4:在文件中的指定行之前插入一行内容。
$ sed '3i\
> Solaris - Sysadmin, Recovery etc.' info.txt
Linux - Sysadmin
Databased - Oracle, MySQL etc
Solaris - Sysadmin, Recovery etc.
Security - Firewall, Network, Oline Security etc
Cool - Websities
Storage - NetApp, EMC etc
Productivity - Too many technologies to explore, no much time available
实例5:在匹配指定模式的行之前插入一行内容。
在文件info.txt中匹配/Security/的行之前插入两行内容。
$ sed '/Security/i\
> Solaris - Sysadmin, Recovery etc.\
> Windows - Sysadmin etc.' info.txt
Linux - Sysadmin
Databased - Oracle, MySQL etc
Solaris - Sysadmin, Recovery etc.
Windows - Sysadmin etc.
Security - Firewall, Network, Oline Security etc
Cool - Websities
Storage - NetApp, EMC etc
Productivity - Too many technologies to explore, no much time available
实例6:在文件的最后一行之前插入一行内容
$ sed '$i\
> Solaris - Sysadmin, Recovery etc.' info.txt
Linux - Sysadmin
Databased - Oracle, MySQL etc
Security - Firewall, Network, Oline Security etc
Cool - Websities
Storage - NetApp, EMC etc
Solaris - Sysadmin, Recovery etc.
Productivity - Too many technologies to explore, no much time available
2. 更改文件中的指定的行
实例1:修改文件的第一行
$ sed '1c<Change line>' info.txt
<Change line>
Databased - Oracle, MySQL etc
Security - Firewall, Network, Oline Security etc
Cool - Websities
Storage - NetApp, EMC etc
Productivity - Too many technologies to explore, no much time available
实例2:修改匹配指定模式的行
匹配/Cool/的行修改为
$ sed '/Cool/c<Change line>' info.txt
Linux - Sysadmin
Databased - Oracle, MySQL etc
Security - Firewall, Network, Oline Security etc
<Change line>
Storage - NetApp, EMC etc
Productivity - Too many technologies to explore, no much time available
实例3:更改文件的最后一行。
$ sed '$c<Change line>' info.txt
Linux - Sysadmin
Databased - Oracle, MySQL etc
Security - Firewall, Network, Oline Security etc
Cool - Websities
Storage - NetApp, EMC etc
<Change line>
3. 删除文件中的行
实例1:删除文件中的指定的行
删除文件所在的第四行
$ sed '4d' info.txt
1 Linux - Sysadmin
2 Databased - Oracle, MySQL etc
3 Security - Firewall, Network, Oline Security etc
5 Storage - NetApp, EMC etc
6 Productivity - Too many technologies to explore, no much time available
实例2:从指定的行开始删除,并每个固定行删除一行
从第四行开始删除,并每隔两行就删掉一行
$ sed '4~2d' info.txt
1 Linux - Sysadmin
2 Databased - Oracle, MySQL etc
3 Security - Firewall, Network, Oline Security etc
5 Storage - NetApp, EMC etc
实例3:删除指定范围的行
$ sed '3,6d' info.txt
1 Linux - Sysadmin
2 Databased - Oracle, MySQL etc
实例4:删除指定范围以外的行
删除3到6行以外的行
$ sed '3,6!d' info.txt
3 Security - Firewall, Network, Oline Security etc
4 Cool - Websities
5 Storage - NetApp, EMC etc
6 Productivity - Too many technologies to explore, no much time available
实例5:删除文件中的最后一行
$ sed '$d' info.txt
1 Linux - Sysadmin
2 Databased - Oracle, MySQL etc
3 Security - Firewall, Network, Oline Security etc
4 Cool - Websities
5 Storage - NetApp, EMC etc
实例6:删除文件中匹配指定模式的行。
匹配/Productivity/的行并删除
$ sed '/Productivity/d' info.txt
1 Linux - Sysadmin
2 Databased - Oracle, MySQL etc
3 Security - Firewall, Network, Oline Security etc
4 Cool - Websities
5 Storage - NetApp, EMC etc
实例7:从匹配指定模式的行删到文件的最后一行
$ sed '/Productivity/,$d' info.txt
1 Linux - Sysadmin
2 Databased - Oracle, MySQL etc
3 Security - Firewall, Network, Oline Security etc
4 Cool - Websities
5 Storage - NetApp, EMC etc
实例8:删除文件中匹配指定模式的行,及其后面的n行内容
删除info.txt文件中的/Security/,及其后面的一行
$ sed '/Security/,+1d' info.txt
1 Linux - Sysadmin
2 Databased - Oracle, MySQL etc
5 Storage - NetApp, EMC etc
6 Productivity - Too many technologies to explore, no much time available
实例9:删除文件中的空行
$ sed '/^$/d' info.txt
1 Linux - Sysadmin
2 Databased - Oracle, MySQL etc
3 Security - Firewall, Network, Oline Security etc
4 Cool - Websities
5 Storage - NetApp, EMC etc
6 Productivity - Too many technologies to explore, no much time available
实例10:删除文件中不匹配指定模式的行
删除文件中不匹配/Databased/或/Security/的行
$ sed '/Databased\|Security/!d' info.txt
2 Databased - Oracle, MySQL etc
3 Security - Firewall, Network, Oline Security etc
实例11:删除文件的指定范围内的行中匹配指定模式的行。
删除含有etc的行
$ sed '1,4{/etc/d}' info.txt
1 Linux - Sysadmin
4 Cool - Websities
5 Storage - NetApp, EMC etc
6 Productivity - Too many technologies to explore, no much time available
4. 替换文件中内容
$ cat techClass.txt
1. Network: Route, Switch, Wireless,Communicate, Device
2. Security: Data Protection,Terminal Security,C1oud Security,WEB Security
3. Server: Blade,Mini Computer,Mainframes,HPC,Disaster Recovery, Network
4. Virtualization:Server Virtualization,Storage Virtualization, Desktop Virtualization
5. Database: SQLServer,MySQL,Oracle, DB2
6. OS: Linux, Unix, Windows
实例1:替换一行中第一个匹配模式的字符串
将每一行的第一个/Virtualization替换成/Virt
$ sed 's/Virtualization/Virt./' techClass.txt
1. Network: Route, Switch, Wireless,Communicate, Device
2. Security: Data Protection,Terminal Security,C1oud Security,WEB Security
3. Server: Blade,Mini Computer,Mainframes,HPC,Disaster Recovery, Network
4. Virt.:Server Virtualization,Storage Virtualization, Desktop Virtualization
5. Database: SQLServer,MySQL,Oracle, DB2
6. OS: Linux, Unix, Windows
# Additional class
实例2:替换文件中的匹配指定模式的所有字符串
将每一行的所有/Virtualization替换成/Virt
$ sed 's/Virtualization/Virt./g' techClass.txt
1. Network: Route, Switch, Wireless,Communicate, Device
2. Security: Data Protection,Terminal Security,C1oud Security,WEB Security
3. Server: Blade,Mini Computer,Mainframes,HPC,Disaster Recovery, Network
4. Virt.:Server Virt.,Storage Virt., Desktop Virt.
5. Database: SQLServer,MySQL,Oracle, DB2
6. OS: Linux, Unix, Windows
# Additional class
实例3:替换文件中每行第n个匹配指定模式的字符串
将每一行的第三个/Virtualization替换成/Virt
$ sed 's/Virtualization/Virt./3' techClass.txt
1. Network: Route, Switch, Wireless,Communicate, Device
2. Security: Data Protection,Terminal Security,C1oud Security,WEB Security
3. Server: Blade,Mini Computer,Mainframes,HPC,Disaster Recovery, Network
4. Virtualization:Server Virtualization,Storage Virt., Desktop Virtualization
5. Database: SQLServer,MySQL,Oracle, DB2
6. OS: Linux, Unix, Windows
# Additional class
实例4:将发生字符串替换的行写入指定的文件,并只打印发生替换的行
将文件中的“Network”字符串替换为“Net.”
$ sed -n 's/Network/Net./gpw /tmp/sedOutput' techClass.txt
1. Net.: Route, Switch, Wireless,Communicate, Device
3. Server: Blade,Mini Computer,Mainframes,HPC,Disaster Recovery, Net.
实例5:只替换文件中匹配指定模式的行中的字符串
文件中匹配/:/的行中的逗号“,”之后的字符串清空
$ sed '/:/s/,.*//g' techClass.txt
1. Network: Route
2. Security: Data Protection
3. Server: Blade
4. Virtualization:Server Virtualization
5. Database: SQLServer
6. OS: Linux
# Additional class
实例6:删掉每行的最后n个字符
删掉文件中每行最后两个字符
$ sed 's/..$//g' techClass.txt
1. Network: Route, Switch, Wireless,Communicate, Devic
2. Security: Data Protection,Terminal Security,C1oud Security,WEB Securit
3. Server: Blade,Mini Computer,Mainframes,HPC,Disaster Recovery, Networ
4. Virtualization:Server Virtualization,Storage Virtualization, Desktop Virtualizati
5. Database: SQLServer,MySQL,Oracle, DB
6. OS: Linux, Unix, Window
# Additional cla
实例7:删除文件中的注释
$ sed 's/^#.*//' techClass.txt
1. Network: Route, Switch, Wireless,Communicate, Device
2. Security: Data Protection,Terminal Security,C1oud Security,WEB Security
3. Server: Blade,Mini Computer,Mainframes,HPC,Disaster Recovery, Network
4. Virtualization:Server Virtualization,Storage Virtualization, Desktop Virtualization
5. Database: SQLServer,MySQL,Oracle, DB2
6. OS: Linux, Unix, Windows
实例8:删除文件中的注释及其空行
$ sed 's/^#.*//;/^$/d' techClass.txt
1. Network: Route, Switch, Wireless,Communicate, Device
2. Security: Data Protection,Terminal Security,C1oud Security,WEB Security
3. Server: Blade,Mini Computer,Mainframes,HPC,Disaster Recovery, Network
4. Virtualization:Server Virtualization,Storage Virtualization, Desktop Virtualization
5. Database: SQLServer,MySQL,Oracle, DB2
6. OS: Linux, Unix, Windows
实例9:使用符号“&”获得匹配的字符串
每一行数字编号加一个小括号()
$ sed 's/^[0-9]\./(&)/' techClass.txt
(1.) Network: Route, Switch, Wireless,Communicate, Device
(2.) Security: Data Protection,Terminal Security,C1oud Security,WEB Security
(3.) Server: Blade,Mini Computer,Mainframes,HPC,Disaster Recovery, Network
(4.) Virtualization:Server Virtualization,Storage Virtualization, Desktop Virtualization
(5.) Database: SQLServer,MySQL,Oracle, DB2
(6.) OS: Linux, Unix, Windows
# Additional class
5. 打印文件中的行
实例1:打印文件中的第n行
$ sed -n '4p' techClass.txt
Virtualization:Server Virtualization,Storage Virtualization, Desktop Virtuali zation
实例2:文件从第n行开始打印,并每隔m-1行就打印一行。
$ sed -n '3~2p' techClass.txt
3. Server: Blade,Mini Computer,Mainframes,HPC,Disaster Recovery, Network
5. Database: SQLServer,MySQL,Oracle, DB2
# Additional class
实例3:打印文件的最后一行
$ sed -n '$p' techClass.txt
# Additional class
实例4:打印文件的第n~m行
$ sed -n '2,6p' techClass.txt
2. Security: Data Protection,Terminal Security,C1oud Security,WEB Security
3. Server: Blade,Mini Computer,Mainframes,HPC,Disaster Recovery, Network
4. Virtualization:Server Virtualization,Storage Virtualization, Desktop Virtualization
5. Database: SQLServer,MySQL,Oracle, DB2
6. OS: Linux, Unix, Windows
实例5:打印文件的第n行到最后一行
$ sed -n '3,$p' techClass.txt
3. Server: Blade,Mini Computer,Mainframes,HPC,Disaster Recovery, Network
4. Virtualization:Server Virtualization,Storage Virtualization, Desktop Virtualization
5. Database: SQLServer,MySQL,Oracle, DB2
6. OS: Linux, Unix, Windows
# Additional class
实例6:打印文件中匹配指定模式的行
$ sed -n '/Network/p' techClass.txt
1. Network: Route, Switch, Wireless,Communicate, Device
3. Server: Blade,Mini Computer,Mainframes,HPC,Disaster Recovery, Network
实例7:打印文件中从匹配指定模式的行到第n行的内容
$ sed -n '/Security/,6p' techClass.txt
2. Security: Data Protection,Terminal Security,C1oud Security,WEB Security
3. Server: Blade,Mini Computer,Mainframes,HPC,Disaster Recovery, Network
4. Virtualization:Server Virtualization,Storage Virtualization, Desktop Virtualization
5. Database: SQLServer,MySQL,Oracle, DB2
6. OS: Linux, Unix, Windows
实例8:打印文件中从第n行道匹配指定模式的行的内容
$ sed -n '1,/Database/p' techClass.txt
1. Network: Route, Switch, Wireless,Communicate, Device
2. Security: Data Protection,Terminal Security,C1oud Security,WEB Security
3. Server: Blade,Mini Computer,Mainframes,HPC,Disaster Recovery, Network
4. Virtualization:Server Virtualization,Storage Virtualization, Desktop Virtualization
5. Database: SQLServer,MySQL,Oracle, DB2
实例9:打印文件中从匹配指定模式的行到最后一行
$ sed -n '/Server/,$p' techClass.txt
3. Server: Blade,Mini Computer,Mainframes,HPC,Disaster Recovery, Network
4. Virtualization:Server Virtualization,Storage Virtualization, Desktop Virtualization
5. Database: SQLServer,MySQL,Oracle, DB2
6. OS: Linux, Unix, Windows
# Additional class
实例10:打印文件中匹配指定模式的行及后面的n行
$ sed -n '/Network/,+1p' techClass.txt
1. Network: Route, Switch, Wireless,Communicate, Device
2. Security: Data Protection,Terminal Security,C1oud Security,WEB Security
3. Server: Blade,Mini Computer,Mainframes,HPC,Disaster Recovery, Network
4. Virtualization:Server Virtualization,Storage Virtualization, Desktop Virtualization
实例11:打印文件中从匹配模式的行到匹配另一个指定模式的行的内容
$ sed -n '/Security/,/OS/p' techClass.txt
2. Security: Data Protection,Terminal Security,C1oud Security,WEB Security
3. Server: Blade,Mini Computer,Mainframes,HPC,Disaster Recovery, Network
4. Virtualization:Server Virtualization,Storage Virtualization, Desktop Virtualization
5. Database: SQLServer,MySQL,Oracle, DB2
6. OS: Linux, Unix, Windows