博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unix Sed Tutorial 5: How To Execute Multiple Sed Commands
阅读量:2218 次
发布时间:2019-05-08

本文共 2107 字,大约阅读时间需要 7 分钟。

Question: Is it possible for me to combine multiple sed commands? Can I combine two sed commands and execute it as single sed command?

Answer: In our previous articles we learned sed with single commands — , , and .

In this article let us review how to combine multiple sed commands using option -e as shown below.

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.

Let us first create thegeekstuff.txt file that will be used in all the examples mentioned below.

# cat thegeekstuff.txt1. Linux - Sysadmin, Scripting etc.2. Databases - Oracle, mySQL etc.3. Hardware4. Security (Firewall, Network, Online Security etc)5. Storage6. Cool gadgets and websites7. Productivity (Too many technologies to explore, not much time available)8. Website Design9. Software Development10.Windows- Sysadmin, reboot etc.

1.Delete 4th and 2nd line from the input

This sed example deletes 4th and 2nd line from the file thegeekstuff.txt. Using “-e” option, you can give any number of commands with sed.

$ sed -e '4d' -e '2d' thegeekstuff.txt1. Linux - Sysadmin, Scripting etc.3. Hardware5. Storage6. Cool gadgets and websites7. Productivity (Too many technologies to explore, not much time available)8. Website Design9. Software Development10.Windows- Sysadmin, reboot etc.

2. Print the lines which matches the pattern1 and lines matches pattern2

This sed example prints all lines that matches either the pattern “Storage” or “Software”.

$ sed -n  -e '/Software/p'  -e '/Storage/p'  thegeekstuff.txt5. Storage9. Software Development

3. Delete the first,last and all the blank lines from input

This sed example deletes the first line, last line and all the blank lines from input file.

$ sed -e '1d' -e '$d' -e '/^$/d' thegeekstuff.txt2. Databases - Oracle, mySQL etc.3. Hardware4. Security (Firewall, Network, Online Security etc)5. Storage6. Cool gadgets and websites7. Productivity (Too many technologies to explore, not much time available)8. Website Design9. Software Development

转载地址:http://zyffb.baihongyu.com/

你可能感兴趣的文章
Java网络编程与NIO详解8:浅析mmap和Direct Buffer
查看>>
Java网络编程与NIO详解10:深度解读Tomcat中的NIO模型
查看>>
Java网络编程与NIO详解11:Tomcat中的Connector源码分析(NIO)
查看>>
深入理解JVM虚拟机1:JVM内存的结构与消失的永久代
查看>>
深入理解JVM虚拟机3:垃圾回收器详解
查看>>
深入理解JVM虚拟机4:Java class介绍与解析实践
查看>>
深入理解JVM虚拟机5:虚拟机字节码执行引擎
查看>>
深入理解JVM虚拟机6:深入理解JVM类加载机制
查看>>
深入了解JVM虚拟机8:Java的编译期优化与运行期优化
查看>>
深入理解JVM虚拟机9:JVM监控工具与诊断实践
查看>>
深入理解JVM虚拟机10:JVM常用参数以及调优实践
查看>>
深入理解JVM虚拟机12:JVM性能管理神器VisualVM介绍与实战
查看>>
深入理解JVM虚拟机13:再谈四种引用及GC实践
查看>>
Spring源码剖析1:Spring概述
查看>>
Spring源码剖析2:初探Spring IOC核心流程
查看>>
Spring源码剖析5:JDK和cglib动态代理原理详解
查看>>
Spring源码剖析6:Spring AOP概述
查看>>
Spring源码剖析8:Spring事务概述
查看>>
Spring源码剖析9:Spring事务源码剖析
查看>>
重新学习Mysql数据库1:无废话MySQL入门
查看>>