Linux脚本编写之sed用法
来源:互联网 发布:数据库接口什么样的 编辑:程序博客网 时间:2024/05/16 04:43
sed命令行格式为:
sed [-nefri] ‘command’ 输入文本
常用选项:
-n:使用安静(silent)模式。在一般 sed 的用法中,所有来自STDIN的资料一般都会被列出到屏幕上。但如果加上 -n 参数后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来。
-e∶直接在指令列模式上进行 sed 的动作编辑;
-f∶直接将 sed 的动作写在一个档案内, -f filename 则可以执行 filename 内的sed 动作;
-r∶sed的动作支持延伸型正规表示法的语法。(预设是基础正规表示法语法);
-i∶直接修改读取的档案内容,不由屏幕输出。
常用命令:
a ∶新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
c ∶取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
d ∶删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
i ∶插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
p ∶列印,亦即将某个选择的资料印出。通常 p 会与参数 sed -n 一起运作~
s ∶取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是啦!
增加一行或多行字符串
[root@rhel6 ~]# sed -i '1a 172.24.11.20 rhel6' /etc/hosts#第一行后增加“172.24.11.20 rhel6”
[root@rhel6 ~]# sed -i '1i 172.24.11.10 rhel5' /etc/hosts #第一行前面增加“172.24.11.10 rhel5”
[root@rhel6 ~]# sed -i '$a #add by id12345\n192.168.100.1 test1' /etc/hosts #最后一行后增加多行,使用换行符\n
代换一行或多行
[root@rhel6 ~]# sed -i '1,3c ####' /etc/hosts #第一行到第三行代换为一行###
对文件添加一些内容,继续实验
替换一行中的某部分
格式:sed 's/要替换的字符串/新的字符串/g' (要替换的字符串可以用正则表达式)
- Linux脚本编写之sed用法
- linux之sed用法
- linux之sed用法
- linux之sed用法
- linux之sed用法
- linux之sed用法
- linux之sed用法
- linux之sed用法
- linux之sed用法
- linux之sed用法
- linux之sed用法
- linux之sed用法
- linux之sed用法
- linux之sed用法
- linux之sed用法
- linux之sed用法
- linux之sed用法
- linux之sed用法
- Linux下NAO机器人开发环境搭建完全教程(Version1.0)
- Xcode6中自动布局autolayout和sizeclass的使用
- JUnit单元测试化工具的学习
- android01-快速入门
- HDU 5201 The Monkey King 组合数+不定方程解的个数
- Linux脚本编写之sed用法
- CF 168 B Zero Tree(树形dp)
- 编程控制分屏显示
- LeetCode题解:House Robber
- HBase之Java API
- 导入项目出现AppBaseTheme找不到的情况解决办法
- HTML5语言的28个新特性学习
- RMAN中常用backup命令
- Oracle RAC 客户端连接负载均衡(Load Balance)