首页 > 代码库 > sed系列:多命令执行

sed系列:多命令执行

Syntax: 
#sed -e ‘command‘ -e ‘command‘ filename 
Note: -e option is optional for sed with single command. sed will execute the each set of command while processing input from the pattern buffer. 
让我们先创建示例文件 
# cat thegeekstuff.txt 
1. Linux - Sysadmin, Scripting etc. 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
4. Security (Firewall, Network, Online Security etc) 
5. Storage 
6. Cool gadgets and websites 
7. Productivity (Too many technologies to explore, not much time available) 
8. Website Design 
9. Software Development 

10.Windows- Sysadmin, reboot etc

例1:删除第1行和第4行 
$sed -e ‘4d‘ -e ‘2d‘ thegeekstuff.txt 

1. Linux - Sysadmin, Scripting etc. 
3. Hardware 
5. Storage 
6. Cool gadgets and websites 
7. Productivity (Too many technologies to explore, not much time available) 
8. Website Design 
9. Software Development 

10.Windows- Sysadmin, reboot etc. 


例2:输出模式匹配1和模式匹配行2 
$sed -n -e ‘/Storage/p‘ -e /Software/p thegeekstuff.txt 

5. Storage 
9. Software Development 


例3:删除第一行,最后一行和空行 
$sed -e ‘1d‘ -e ‘$d‘ ‘/^$/d‘ thegeekstuff.txt 

sed: can‘t read /^$/d: No such file or directory 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
4. Security (Firewall, Network, Online Security etc) 
5. Storage 
6. Cool gadgets and websites 
7. Productivity (Too many technologies to explore, not much time available) 
8. Website Design 
9. Software Development 
$sed ‘ 
> 1d 
> $d 
> /^$/d 
> ‘ thegeekstuff.txt 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
4. Security (Firewall, Network, Online Security etc) 
5. Storage 
6. Cool gadgets and websites 
7. Productivity (Too many technologies to explore, not much time available) 
8. Website Design 
9. Software Development 
$

sed系列:多命令执行