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可以实现自动化管理,让系统管理员的工作 ...
随机推荐
- machine learning for hacker记录(1) R与机器学习
开篇:首先这本书的名字很霸气,全书内容讲的是R语言在机器学习上面的应用,一些基本的分类算法(tree,SVM,NB),回归算法,智能优化算法,维度约减等,机器学习领域已经有很多成熟的R工具箱,毕竟这个 ...
- Java其实不支持垃圾回收
Java其实不支持垃圾回收.如果真的支持的话,大多数Java程序在运行的一开始就应该把程序本身删除,因为这些程序本身就是垃圾. // TODO: This is a 分割线. Please no ...
- 2018年东北农业大学春季校赛 B wyh的矩阵【规律】
题目链接 https://www.nowcoder.com/acm/contest/93/B 思路 先加入 中间的那行 和中间的那列 再减去 最中间那个数 因为它 加了两次 然后逐行往下加 会发现是一 ...
- RabbitMQ的介绍与spring整合
本文主要讲述的是个人参考官网及其他前辈博客,对RabbitMQ的一些理解与spring整个RabbitMQ. 一.RabbitMQ的介绍 1.1.什么是RabbitMQ RabbitMQ是一个由erl ...
- oracle添加表注释和表字段注释
创建Oracle数据库表时加上注释 CREATE TABLE t1( id varchar2(32) primary key,name VARCHAR2(8) NOT NULL, age numbe ...
- Android Jni(Java Native Interface)笔记
首先记录一个问题,关于如何用javah生成头文件. 为什么要生成头文件?在含有 static{ System.loadLibrary("hellojni"); } 这样代码的类下面 ...
- ORA-12547: TNS:lost contact
碰到这个ORA-12547: TNS:lost contact的问题,翻了很多资料和METALINK,总结了一下原因: 1 是由于rpm包没有安装,对于我们的生产环境,此包是安装的. admin@p1 ...
- 【USACO】Optimal Milking
题目链接 : [POJ]点击打开链接 [caioj]点击打开链接 算法 : 1:跑一遍弗洛伊德,求出点与点之间的最短路径 2:二分答案,二分”最大值最小“ 3.1:建边,将 ...
- 单次目标检测器-YOLO简介
YOLO 在卷积层之后使用了 DarkNet 来做特征检测. 然而,它并没有使用多尺度特征图来做独立的检测.相反,它将特征图部分平滑化,并将其和另一个较低分辨率的特征图拼接.例如,YOLO 将一个 2 ...
- HttpClient入门教程
HttpClient使用详解与实战一:https://www.jianshu.com/p/375be5929bed