[Linux Shell学习系列十四]sed和awk-3.sed命令实例

河的第三条岸 2020-06-12

D27

1. 向文件中添加或插入行

1)在文件的指定行后添加一行内容

$ cat info.txt 
Linux - Sysadmin
Database - Oracle, MySQL etc.
Security - Firewall, Network, Online Security etc.
Cool - Websites

$ sed ‘3a\ 
> Solaries - Sysadmin, Recovery etc.‘ info.txt
Linux - Sysadmin
Database - Oracle, MySQL etc.
Security - Firewall, Network, Online Security etc.
Solaries - Sysadmin, Recovery etc.
Cool - Websites

2)在匹配模式的行之后添加一行内容

$ sed ‘/Database/a\
Solaries - Sysadmin, Recovery etc.‘ info.txt
Linux - Sysadmin
Database - Oracle, MySQL etc.
Solaries - Sysadmin, Recovery etc.
Security - Firewall, Network, Online Security etc.
Cool - Websites

3)在文件的最后一行后添加多行内容

$ sed ‘$a\ 
Solaries - Sysadmin, Recovery etc.Windows - Sysadmin etc.‘ info.txt
Linux - Sysadmin
Database - Oracle, MySQL etc.
Security - Firewall, Network, Online Security etc.
Cool - Websites
Solaries - Sysadmin, Recovery etc.
Windows - Sysadmin etc.

4)在文件的指定行之前插入一行内容

$ sed ‘3i\
> Solaries - Sysadmin, Recovery etc.‘ info.txt
Linux - Sysadmin
Database - Oracle, MySQL etc.
Solaries - Sysadmin, Recovery etc.
Security - Firewall, Network, Online Security etc.
Cool - Websites

5)在匹配指定模式的行之前插入多行内容

$ sed ‘/Security/i\
> Solaries - Sysadmin, Recovery etc.> Windows - Sysadmin etc.‘ info.txt
Linux - Sysadmin
Database - Oracle, MySQL etc.
Solaries - Sysadmin, Recovery etc.
Windows - Sysadmin etc.
Security - Firewall, Network, Online Security etc.
Cool - Websites

6)在文件的最后一行之前插入一行内容

$ sed ‘$i\
> Solaries - Sysadmin, Recovery etc.‘ info.txt
Linux - Sysadmin
Database - Oracle, MySQL etc.
Security - Firewall, Network, Online Security etc.
Solaries - Sysadmin, Recovery etc.
Cool - Websites

2. 更改文件中指定的行

1)修改文件中的第一行

$ sed ‘1c<Change line 1>‘ info.txt
<Change line 1>
Database - Oracle, MySQL etc.
Security - Firewall, Network, Online Security etc.
Cool - Websites

2)修改匹配指定模式的行

$ sed ‘/Cool/c<Change line Cool>‘ info.txt 
Linux - Sysadmin
Database - Oracle, MySQL etc.
Security - Firewall, Network, Online Security etc.
<Change line Cool>

3)更改文件的最后一行

$ sed ‘$c<Change last line>‘ info.txt 
Linux - Sysadmin
Database - Oracle, MySQL etc.
Security - Firewall, Network, Online Security etc.
<Change last line>

3. 删除文件中的行

$ cat info_num.txt 
1.      Linux - Sysadmin
2.      Database - Oracle, MySQL etc.
3.      Security - Firewall, Network, Online Security etc.
4.      Cool - Websites

#1)删除文件中指定的行
$ sed ‘4d‘ info_num.txt 
1.      Linux - Sysadmin
2.      Database - Oracle, MySQL etc.
3.      Security - Firewall, Network, Online Security etc.

#2)从指定的行开始删除,每隔固定的行数删除一行。
#从第一行开始,每隔两行删除一行
$ sed ‘1~2d‘ info_num.txt 
2.      Database - Oracle, MySQL etc.
4.      Cool - Websites

#3)删除指定范围内的行
$ sed ‘3,4d‘ info_num.txt 
1.      Linux - Sysadmin
2.      Database - Oracle, MySQL etc.

#4)删除指定范围以外的行
$ sed ‘3,4!d‘ info_num.txt 
3.      Security - Firewall, Network, Online Security etc.
4.      Cool - Websites

#5)删除文件的最后一行
$ sed ‘$d‘ info_num.txt 
1.      Linux - Sysadmin
2.      Database - Oracle, MySQL etc.
3.      Security - Firewall, Network, Online Security etc.

#6)删除文件中匹配指定模式的行
$ sed ‘/Linux/d‘ info_num.txt 
2.      Database - Oracle, MySQL etc.
3.      Security - Firewall, Network, Online Security etc.
4.      Cool - Websites

#7)从指定模式的行删除,直到文件的最后一行
$ sed ‘/Security/,$d‘ info_num.txt 
1.      Linux - Sysadmin
2.      Database - Oracle, MySQL etc.

#8)删除文件中匹配指定模式的行及其后面n行的内容
$ sed ‘/Database/,+1d‘ info_num.txt 
1.      Linux - Sysadmin
4.      Cool - Websites

#9)删除文件中的空行
$ sed ‘/^$/d‘ info_num.txt 
1.      Linux - Sysadmin
2.      Database - Oracle, MySQL etc.
3.      Security - Firewall, Network, Online Security etc.
4.      Cool - Websites

#10)删除文件中不匹配指定模式的行
$ sed ‘/Database\|Security/!d‘ info_num.txt 
2.      Database - Oracle, MySQL etc.
3.      Security - Firewall, Network, Online Security etc.

#11)删除文件的指定范围内的行中匹配指定模式的行
$ sed ‘1,2{/etc\./d}‘ info_num.txt 
1.      Linux - Sysadmin
3.      Security - Firewall, Network, Online Security etc.
4.      Cool - Websites

4. 替换文件中的内容

$ cat techClass.txt 
1.      Network: Route, Switch, Wireless, Communicate, Device
2.      Security: Data Protection, Terminal Security, Cloud Security, WEB Security
3.      Server: Blade, Mini Computer, Mainframe, HPC, Disaster Recovery, Network
4.      Database: SQLServer, MySQL, Oracle, DB2
5.      OS: Linux, Unix, Windows
#Additional class

#1)替换一行中第一个匹配模式的字符串
#将一行中第一个Security替换为Safety
$ sed ‘s/Security/Safety/‘ techClass.txt 
1.      Network: Route, Switch, Wireless, Communicate, Device
2.      Safety: Data Protection, Terminal Security, Cloud Security, WEB Security
3.      Server: Blade, Mini Computer, Mainframe, HPC, Disaster Recovery, Network
4.      Database: SQLServer, MySQL, Oracle, DB2
5.      OS: Linux, Unix, Windows
#Additional class

#2)替换文件中匹配指定模式的所有字符串
#将一行中所有的Security替换为Safety
$ sed ‘s/Security/Safety/g‘ techClass.txt 
1.      Network: Route, Switch, Wireless, Communicate, Device
2.      Safety: Data Protection, Terminal Safety, Cloud Safety, WEB Safety
3.      Server: Blade, Mini Computer, Mainframe, HPC, Disaster Recovery, Network
4.      Database: SQLServer, MySQL, Oracle, DB2
5.      OS: Linux, Unix, Windows
#Additional class

#3)替换文件中每行第n个匹配指定模式的字符串
#将一行中第三个Security替换为Safety
$ sed ‘s/Security/Safety/3‘ techClass.txt 
1.      Network: Route, Switch, Wireless, Communicate, Device
2.      Security: Data Protection, Terminal Security, Cloud Safety, WEB Security
3.      Server: Blade, Mini Computer, Mainframe, HPC, Disaster Recovery, Network
4.      Database: SQLServer, MySQL, Oracle, DB2
5.      OS: Linux, Unix, Windows
#Additional class

#4)将发生字符串替换的行写入指定的文件,并只打印发生替换的行
#将Database替换为DB
$ sed -n ‘s/Database/DB/gpw sedOutput.txt‘ techClass.txt 
4.      DB: SQLServer, MySQL, Oracle, DB2

#文件内容
$ cat sedOutput.txt 
4.      DB: SQLServer, MySQL, Oracle, DB2

#5)只替换文件中匹配指定模式的行中的字符串
#将逗号以后的字符串替换为空
$ sed ‘/:/s/,.*//g‘ techClass.txt 
1.      Network: Route
2.      Security: Data Protection
3.      Server: Blade
4.      Database: SQLServer
5.      OS: Linux
#Additional class

#6)删掉每行的最后n个字符
#删除每行的最后2个字符
$ sed ‘s/..$//g‘ techClass.txt 
1.      Network: Route, Switch, Wireless, Communicate, Devi
2.      Security: Data Protection, Terminal Security, Cloud Security, WEB Securi
3.      Server: Blade, Mini Computer, Mainframe, HPC, Disaster Recovery, Netwo
4.      Database: SQLServer, MySQL, Oracle, D
5.      OS: Linux, Unix, Windo
#Additional cla

#7)删除文件中的注释,即以#开头的行替换为空
#注意这里会保留一个空行
$ sed ‘s/^#.*//g‘ techClass.txt 
1.      Network: Route, Switch, Wireless, Communicate, Device
2.      Security: Data Protection, Terminal Security, Cloud Security, WEB Security
3.      Server: Blade, Mini Computer, Mainframe, HPC, Disaster Recovery, Network
4.      Database: SQLServer, MySQL, Oracle, DB2
5.      OS: Linux, Unix, Windows


#8)删除文件中的注释及其空行
#;前面的/^#.*//是将注释行替换为空,;后面的/^$/d是删除空行
$ sed ‘s/^#.*//;/^$/d‘ techClass.txt 
1.      Network: Route, Switch, Wireless, Communicate, Device
2.      Security: Data Protection, Terminal Security, Cloud Security, WEB Security
3.      Server: Blade, Mini Computer, Mainframe, HPC, Disaster Recovery, Network
4.      Database: SQLServer, MySQL, Oracle, DB2
5.      OS: Linux, Unix, Windows

#9)使用符号&获得匹配的字符串
#匹配的模式是:数字.,将匹配的字符串加上括号
$ sed ‘s/^[0-9]\./(&)/‘ techClass.txt 
(1.)    Network: Route, Switch, Wireless, Communicate, Device
(2.)    Security: Data Protection, Terminal Security, Cloud Security, WEB Security
(3.)    Server: Blade, Mini Computer, Mainframe, HPC, Disaster Recovery, Network
(4.)    Database: SQLServer, MySQL, Oracle, DB2
(5.)    OS: Linux, Unix, Windows
#Additional class

#与上面的命令结果相同
#通过()获取字符串(()需要转义)
$ sed ‘s/\(^[0-9]\.\)/(\1)/‘ techClass.txt 
(1.)    Network: Route, Switch, Wireless, Communicate, Device
(2.)    Security: Data Protection, Terminal Security, Cloud Security, WEB Security
(3.)    Server: Blade, Mini Computer, Mainframe, HPC, Disaster Recovery, Network
(4.)    Database: SQLServer, MySQL, Oracle, DB2
(5.)    OS: Linux, Unix, Windows
#Additional class

5. 打印文件中的行

$ cat techClass.txt 
1.      Network: Route, Switch, Wireless, Communicate, Device
2.      Security: Data Protection, Terminal Security, Cloud Security, WEB Security
3.      Server: Blade, Mini Computer, Mainframe, HPC, Disaster Recovery, Network
4.      Database: SQLServer, MySQL, Oracle, DB2
5.      OS: Linux, Unix, Windows
#Additional class

#1)打印文件中第n行的内容
#打印第4行
$ sed -n ‘4p‘ techClass.txt 
4.      Database: SQLServer, MySQL, Oracle, DB2

#2)从第n行开始,每m行打印一行(每隔m-1行)
#从第1行开始,每2行打印一行
$ sed -n ‘1~2p‘ techClass.txt 
1.      Network: Route, Switch, Wireless, Communicate, Device
3.      Server: Blade, Mini Computer, Mainframe, HPC, Disaster Recovery, Network
5.      OS: Linux, Unix, Windows

#3)打印文件的最后一行的内容
$ sed -n ‘$p‘ techClass.txt 
#Additional class

#4)打印文件的第n~m行的内容
#打印第2到4行
$ sed -n ‘2,4p‘ techClass.txt 
2.      Security: Data Protection, Terminal Security, Cloud Security, WEB Security
3.      Server: Blade, Mini Computer, Mainframe, HPC, Disaster Recovery, Network
4.      Database: SQLServer, MySQL, Oracle, DB2

#5)打印文件的第n行到最后一行的内容
#打印第3行到最后一行
$ sed -n ‘3,$p‘ techClass.txt 
3.      Server: Blade, Mini Computer, Mainframe, HPC, Disaster Recovery, Network
4.      Database: SQLServer, MySQL, Oracle, DB2
5.      OS: Linux, Unix, Windows
#Additional class

#6)打印文件中匹配指定模式的行的内容
#匹配Network的行
$ sed -n ‘/Network/p‘ techClass.txt 
1.      Network: Route, Switch, Wireless, Communicate, Device
3.      Server: Blade, Mini Computer, Mainframe, HPC, Disaster Recovery, Network

#7)打印文件中从匹配模式的行到指定行的内容
#匹配Network的行到第4行
$ sed -n ‘/Network/,4p‘ techClass.txt 
1.      Network: Route, Switch, Wireless, Communicate, Device
2.      Security: Data Protection, Terminal Security, Cloud Security, WEB Security
3.      Server: Blade, Mini Computer, Mainframe, HPC, Disaster Recovery, Network
4.      Database: SQLServer, MySQL, Oracle, DB2

#8)打印文件中从第n行到匹配指定模式的行
#打印第1行到匹配Security的行
$ sed -n ‘1,/Security/p‘ techClass.txt 
1.      Network: Route, Switch, Wireless, Communicate, Device
2.      Security: Data Protection, Terminal Security, Cloud Security, WEB Security

#9)打印文件中从匹配指定模式的行到最后一行的内容
$ sed -n ‘/Server/,$p‘ techClass.txt 
3.      Server: Blade, Mini Computer, Mainframe, HPC, Disaster Recovery, Network
4.      Database: SQLServer, MySQL, Oracle, DB2
5.      OS: Linux, Unix, Windows
#Additional class

#10)打印文件中匹配指定模式的行及其后面的n行
#匹配Network的行及其后面一行,因为第1行和第3行都是匹配的行,所以打印了4行
$ sed -n ‘/Network/,+1p‘ techClass.txt 
1.      Network: Route, Switch, Wireless, Communicate, Device
2.      Security: Data Protection, Terminal Security, Cloud Security, WEB Security
3.      Server: Blade, Mini Computer, Mainframe, HPC, Disaster Recovery, Network
4.      Database: SQLServer, MySQL, Oracle, DB2

#如果是打印匹配Network的行及其后面两行,则只打印3行
$ sed -n ‘/Network/,+2p‘ techClass.txt 
1.      Network: Route, Switch, Wireless, Communicate, Device
2.      Security: Data Protection, Terminal Security, Cloud Security, WEB Security
3.      Server: Blade, Mini Computer, Mainframe, HPC, Disaster Recovery, Network

#11)打印文件从匹配指定模式的行到匹配另一个模式的行
#从匹配Security的行,直到匹配DB的行
$ sed -n ‘/Security/,/DB/p‘ techClass.txt 
2.      Security: Data Protection, Terminal Security, Cloud Security, WEB Security
3.      Server: Blade, Mini Computer, Mainframe, HPC, Disaster Recovery, Network
4.      Database: SQLServer, MySQL, Oracle, DB2

相关推荐