shell学习(2)- sed
一、替换(s)
命令 | 说明 |
n | 1到512之间的一个数字,表示文本模式中指定模式第n次出现的情况进行替换 |
g | 对模式空间的所有出现的情况进行全局更改,没有g通常只有第一次出现的情况被取代 |
p | 打印模式空间的内容 |
w file | 将模式空间的内容写到文件file中 |
x | 表示互换模板块中的文本和缓冲区中的文本 |
y | 表示把一个字符翻译为另外的字符(但是不用于正则表达式) |
& | 已匹配字符串标记 |
[kumufengchun@localhost ~]$ cat replace.txt
my name is Lily
This is your dog
my dog's name is frank
This is your fish
my fish's name is george
This is your goat
my goat's name is adam
替换文本中的字符串
[kumufengchun@localhost ~]$ sed 's/This/That/' replace.txt
my name is Lily
That is your dog
my dog's name is frank
That is your fish
my fish's name is george
That is your goat
my goat's name is adam
上述命令只是替换每行的第一个,如果替换所有的需要 用 sed 's/This/That/g' replace.txt
打印替换行
[kumufengchun@localhost ~]$ sed 's/This/That/p' replace.txt
my name is Lily
That is your dog
That is your dog
my dog's name is frank
That is your fish
That is your fish
my fish's name is george
That is your goat
That is your goat
my goat's name is adam
[kumufengchun@localhost ~]$ sed -n 's/This/That/p' replace.txt
That is your dog
That is your fish
That is your goat
直接编辑文件选项-i,会匹配文件中每一行的第一个This替换为That,即在原文件中修改
[kumufengchun@localhost ~]$ sed -i 's/This/That/' replace.txt
[kumufengchun@localhost ~]$ cat replace.txt
my name is Lily
That is your dog
my dog's name is frank
That is your fish
my fish's name is george
That is your goat
my goat's name is adam
[kumufengchun@localhost ~]$ cat replace.txt
my name is Lily
that is your that dog
my dog's name is frank
that is your that fish
my fish's name is george
that is your that goat
my goat's name is adam [kumufengchun@localhost ~]$ sed 's/that/this/2g' replace.txt
my name is Lily
that is your this dog
my dog's name is frank
that is your this fish
my fish's name is george
that is your this goat
my goat's name is adam
定界符出现在样式内部时,需要进行转义
[kumufengchun@localhost ~]$ echo '/usr/local/bin' | sed 's/\/usr\/local\/bin/\/USR\/LOCAL\/BIN/g'
/USR/LOCAL/BIN
命令中的三根斜线分隔符可以换成别的符号,有时候替换目录字符串的时候有较多斜线,这个时候换成其它的分割符是较为方便,只需要紧跟s定义即可
[kumufengchun@localhost ~]$ sed 's?that?this?2g' replace.txt
my name is Lily
that is your this dog
my dog's name is frank
that is your this fish
my fish's name is george
that is your this goat
my goat's name is adam
[kumufengchun@localhost ~]$ sed 's|that|this|2g' replace.txt
my name is Lily
that is your this dog
my dog's name is frank
that is your this fish
my fish's name is george
that is your this goat
my goat's name is adam
[kumufengchun@localhost ~]$ sed 's/^/添加的头部&/g' replace.txt
添加的头部my name is Lily
添加的头部that is your that dog
添加的头部my dog's name is frank
添加的头部that is your that fish
添加的头部my fish's name is george
添加的头部that is your that goat
添加的头部my goat's name is adam [kumufengchun@localhost ~]$ sed 's/$/&添加的尾部/g' replace.txt
my name is Lily添加的尾部
that is your that dog添加的尾部
my dog's name is frank添加的尾部
that is your that fish添加的尾部
my fish's name is george添加的尾部
that is your that goat添加的尾部
my goat's name is adam添加的尾部 [kumufengchun@localhost ~]$ sed '2s/that/jiao/g' replace.txt
my name is Lily
jiao is your jiao dog
my dog's name is frank
that is your that fish
my fish's name is george
that is your that goat
my goat's name is adam [kumufengchun@localhost ~]$ sed '$s/my/your/g' replace.txt
my name is Lily
that is your that dog
my dog's name is frank
that is your that fish
my fish's name is george
that is your that goat
your goat's name is adam [kumufengchun@localhost ~]$ sed '2,5s/my/your/g' replace.txt
my name is Lily
that is your that dog
your dog's name is frank
that is your that fish
your fish's name is george
that is your that goat
my goat's name is adam [kumufengchun@localhost ~]$ sed '2,$s/my/your/g' replace.txt
my name is Lily
that is your that dog
your dog's name is frank
that is your that fish
your fish's name is george
that is your that goat
your goat's name is adam
同时执行两个替换规则,sed 's/^/添加的头部&/g;s/$/&添加的尾部/g'
[kumufengchun@localhost ~]$ sed 's/^/添加的头部&/g;s/$/&添加的尾部/g' replace.txt
添加的头部my name is Lily添加的尾部
添加的头部that is your that dog添加的尾部
添加的头部my dog's name is frank添加的尾部
添加的头部that is your that fish添加的尾部
添加的头部my fish's name is george添加的尾部
添加的头部that is your that goat添加的尾部
添加的头部my goat's name is adam添加的尾部
二、删除(d)
[kumufengchun@localhost ~]$ cat sed.txt
my cat's name is betty This is your This dog
my dog's name is This frank This is your fish
my fish's name is This george
This is your goat
my goat's name is This adam [kumufengchun@localhost ~]$ sed '/^$/d' sed.txt
my cat's name is betty
This is your This dog
my dog's name is This frank
This is your fish
my fish's name is This george
This is your goat
my goat's name is This adam
删除第2行
[kumufengchun@localhost ~]$ sed '2d' sed.txt
my cat's name is betty
This is your This dog
my dog's name is This frank This is your fish
my fish's name is This george
This is your goat
my goat's name is This adam
删除文件的第2行到末尾所有行
[kumufengchun@localhost ~]$ sed '2,$d' sed.txt
my cat's name is betty
删除文件最后一行
[kumufengchun@localhost ~]$ sed '$d' sed.txt
my cat's name is betty This is your This dog
my dog's name is This frank This is your fish
my fish's name is This george
This is your goat
删除文件中所有以my开头的行
[kumufengchun@localhost ~]$ sed '/^my/d' sed.txt This is your This dog This is your fish
This is your goat
-e选项允许在同一行里执行多条命令
[kumufengchun@localhost ~]$ sed -e '1,5d' -e 's/my/MY/' sed.txt
This is your fish
MY fish's name is This george
This is your goat
MY goat's name is This adam
删除不连续的行 ##注意这里不能添加引号或者双引号,否则报错
[kumufengchun@localhost ~]$ sed -e{,}d sed.txt
my cat's name is betty
This is your This dog This is your fish
my fish's name is This george
This is your goat
my goat's name is This adam
三、列表(l)
[kumufengchun@localhost ~]$vim list.txt
19楼浙江家博会^I^A19lou.com^A^A^A$
19楼 装修^I^A19lou.com^A^A^A$
杭州19楼家博会^I^A19lou.com^A^A^A$
杭州19楼家博会下一季时间^I^A19lou.com^A^A^A$
杭州19楼装修^I^A19lou.com^A^A^A$
杭州19楼装饰^I^A19lou.com^A^A^A$
杭州装修19楼^I^A19lou.com^A^A^A$
[kumufengchun@localhost ~]$sed "l" list.txt
四、追加(a)
[kumufengchun@localhost ~]$ cat sed.txt
my cat's name is betty This is your This dog
my dog's name is This frank This is your fish
my fish's name is This george
This is your goat
my goat's name is This adam
[kumufengchun@localhost ~]$ sed '/^my/a\what is it' sed.txt
my cat's name is betty
what is it This is your This dog
my dog's name is This frank
what is it This is your fish
my fish's name is This george
what is it
This is your goat
my goat's name is This adam
what is it
在sed.txt文件第2行之后插入what is it
[kumufengchun@localhost ~]$ sed '2a\what is it' sed.txt
my cat's name is betty what is it
This is your This dog
my dog's name is This frank This is your fish
my fish's name is This george
This is your goat
my goat's name is This adam
五、插入(i)
[kumufengchun@localhost ~]$ sed '/^my/i\what is it' sed.txt
what is it
my cat's name is betty This is your This dog
what is it
my dog's name is This frank This is your fish
what is it
my fish's name is This george
This is your goat
what is it
my goat's name is This adam
六、更改(c)
[kumufengchun@localhost ~]$ sed '/^my/c\what is it' sed.txt
what is it This is your This dog
what is it This is your fish
what is it
This is your goat
what is it
七、变形(y)
[kumufengchun@localhost ~]$ sed 'y/my/MY/' sed.txt
MY cat's naMe is bettY This is Your This dog
MY dog's naMe is This frank This is Your fish
MY fish's naMe is This george
This is Your goat
MY goat's naMe is This adaM
八、奇数行
[kumufengchun@localhost ~]$ sed -n 'p;n' sed.txt
my cat's name is betty
This is your This dog my fish's name is This george
my goat's name is This adam
九、偶数行
[kumufengchun@localhost ~]$ sed -n 'n;p' sed.txt my dog's name is This frank
This is your fish
This is your goat
十、从文件读入 (r)
[kumufengchun@localhost ~]$ cat kumu.txt
mmmmmmmmmmmmmmmmmmm
[kumufengchun@localhost ~]$ sed '/my/r kumu.txt' sed.txt
my cat's name is betty
mmmmmmmmmmmmmmmmmmm This is your This dog
my dog's name is This frank
mmmmmmmmmmmmmmmmmmm This is your fish
my fish's name is This george
mmmmmmmmmmmmmmmmmmm
This is your goat
my goat's name is This adam
mmmmmmmmmmmmmmmmmmm
十一、写入文件命令(w)
[kumufengchun@localhost ~]$ sed '/my/w kumu.txt' sed.txt
my cat's name is betty This is your This dog
my dog's name is This frank This is your fish
my fish's name is This george
This is your goat
my goat's name is This adam
[kumufengchun@localhost ~]$ cat kumu.txt
my cat's name is betty
my dog's name is This frank
my fish's name is This george
my goat's name is This adam
十二、命令写入文件 (f)
[kumufengchun@localhost ~]$ cat kumu.txt
s/betty/kumufengchun/g
s/adam/jiaojiao/g [kumufengchun@localhost ~]$ cat sed.txt | sed -f kumu.txt
my cat's name is kumufengchun This is your This dog
my dog's name is This frank This is your fish
my fish's name is This george
This is your goat
my goat's name is This jiaojiao
shell学习(2)- sed的更多相关文章
- 【Linux】shell学习之sed
sed替换命令 使用该命令,可以将特定字符串或匹配的规则表达式用另一个字符串替换. sed 's/88/--/' filename 将filename每行第一次出现的88用字符串--替换,然后将该文件 ...
- Shell学习:grep, sed, awk命令的练习题
http://www.cnblogs.com/chengmo/archive/2013/01/17/2865479.html 文件:datafileSteve Blenheim:238-923-736 ...
- Shell—学习之心得
由于项目要招聘需要有经验shell开发人员(awk编程),而作为技术面试官(暂时)的我对shell编程不太熟:当然以前也写过一些shell脚本来满足项目的需求—备份环境,数据库(逻辑).假如只是针对a ...
- shell学习笔记
shell学习笔记 .查看/etc/shells,看看有几个可用的Shell . 曾经用过的命令存在.bash_history中,但是~/.bash_history记录的是前一次登录前记录的所有指令, ...
- shell学习指南-阅读笔记
shell学习指南真不是刚开始学习shell应该看得书,虽然其中讲了简单的linux命令,shell语法等,但是每章也有些深入和生僻地方,我想如果我刚学shell看到这样的地方一定会头疼的要死.或许也 ...
- Shell学习之结合正则表达式与通配符的使用(五)
Shell学习之结合正则表达式与通配符的使用 目录 通配符 正则表达式与通配符 通配符 通配符的使用 正则表达式 正则表达式 正则表达式的使用 通配符 正则表达式与通配符 正则表达式用来在文件中匹配符 ...
- shell 学习笔记2-shell-test
一.字符串测试表达式 前面一篇介绍:什么是shell,shell变量请参考: shell 学习笔记1-什么是shell,shell变量 1.字符串测试表达式参数 字符串需要用""引 ...
- shell学习总结之自定义函数
shell学习总结之自定义函数 Myfun (){ echo -n "now i is $i " ! [ "$i" ] && exit ; ec ...
- SHELL学习笔记----IF条件判断,判断条件
SHELL学习笔记----IF条件判断,判断条件 前言: 无论什么编程语言都离不开条件判断.SHELL也不例外. if list then do something here ...
- 【转】shell学习笔记(一)——学习目的性、特殊字符、运算符等
1 学习shell的目的性 写之前我们先来搞清楚为什么要学shell,学习要有目的性 shell简单.灵活.高效,特别适合处理一些系统管理方面的小问题 shell可以实现自动化管理,让系统管理员的工作 ...
随机推荐
- 【智能无线小车系列十】通过USB摄像头实现网络监控功能
如果仅有静态图像可能还不足以满足我们的需求,我们可能会需要用到实时的监控功能.这里介绍一款小应用:motion.motion的功能可强大了,不仅可以将监控的画面通过视频传输,实时展现,更为强大的是,m ...
- listview 使用图片三级缓存图片闪动
- 关于web页自动适配屏幕大小
一.先了解下html5的viewport使用 随着高端手机(Andriod,Iphone,Ipod,WinPhone等)的盛行,移动互联应用开发也越来越受到人们的重视,用html5开发移动应用是最好的 ...
- Intellij IDEA 修改代码后自动编译更新
Intellij IDEA 一些不为人知的技巧 问题描述: Intellij IDEA 调试修改时,改动页面和 java 文件后,无法立刻看到变化,需要手动重启服务. 问题原因: 在 IDEA tom ...
- Android View中滚动相关
方法 scrollTo: (内容的左上角)达到某个地点 scrollBy: 根据当前位置,再移动多少 属性: mScrollX, 以下是文档解释 The offset, in pixels ...
- double转int时精度不一致问题
float和double类型的主要设计目的是为了科学计算和工程计算.它们执行二进制浮点运算,这是为了在广域数值范围上提供较为精确的快速近似计算而精心设计的.然而,它们没有提供完全精确的结果,所以不应该 ...
- codeforces 702A A. Maximum Increase(水题)
题目链接: A. Maximum Increase time limit per test 1 second memory limit per test 256 megabytes input sta ...
- 多线程之:synchonized锁实现的原理<一>
一:java同步的锁类型? --->目前在Java中存在两种锁机制:synchonized和Lock--->Lock接口及其实现类是JDK5增加的内容,其作者是大名鼎鼎的并发专家Doug ...
- AngularJS系统学习之Scope(作用域)
本文出自:https://www.w3ctech.com/topic/1611 看完了没怎么懂, 也许是和别人 原文作者: Nicolas Bhttps://www.w3ctech.com/topi ...
- unity3D +php +数据库
本文只是自己学习 摘录网上前人的学习资料,并非自己写的!!!!!!最近有项目需要用到数据库,网上有不少资料,但是整理后发现,那些资料对于完全没有PHP+MySQL经验的人来说还是很难理解,所以分享一下 ...