一、替换(s)

详细用法  [address]s/pattern/replacement/flags
修饰flag的标志如下:
命令 说明
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
 
-n选项和p命令一起使用表示只打印那些发生替换的行
[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
当需要从第N处匹配开始替换时,可以使用/Ng:
修改下文件如下:
[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
”^”表示行首
”$”符号如果在引号中表示行尾,但是在引号外却表示末行(最后一行)
# 注意这里的 " & " 符号,如果没有 “&”,就会直接将匹配到的字符串替换掉
sed 's/^/添加的头部&/g'      #在所有行首添加
sed 's/$/&添加的尾部/g'      #在所有行末添加
sed '2s/原字符串/替换字符串/g'  #替换第2行
sed '$s/原字符串/替换字符串/g'   #替换最后一行
sed '2,5s/原字符串/替换字符串/g' #替换2到5行
sed '2,$s/原字符串/替换字符串/g' #替换2到最后一行
[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)

 列表命令(l)可用于显示模式空间的内容,将非打印的字符显示为两个数字的ASCII码。可以使用该命令来检测输入中的不可见字符
[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
19楼浙江家博会    19lou.com
19\346\245\274 \350\243\205\344\277\256\t\00119lou.com\001\001\001$
19楼 装修    19lou.com
\346\235\255\345\267\23619\346\245\274\345\256\266\345\215\232\344\
\274\232\t\00119lou.com\001\001\001$
杭州19楼家博会    19lou.com
\346\235\255\345\267\23619\346\245\274\345\256\266\345\215\232\344\
\274\232\344\270\213\344\270\200\345\255\243\346\227\266\351\227\264\
\t\00119lou.com\001\001\001$
杭州19楼家博会下一季时间    19lou.com
\346\235\255\345\267\23619\346\245\274\350\243\205\344\277\256\t\0011\
9lou.com\001\001\001$
杭州19楼装修    19lou.com
\346\235\255\345\267\23619\346\245\274\350\243\205\351\245\260\t\0011\
9lou.com\001\001\001$

四、追加(a)

[line-address]a\text file.txt
追加到以my开头的行后面
[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)

 line-address]i\text file.txt
[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)

 [line-address]c\text file.txt
[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)

 
file里的内容被读进来,显示在与test匹配的行后面,如果匹配多行,则file的内容将显示在所有匹配行的下面
[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

-e:执行命令行中的指令,例如:sed -e 'command' file(s)
 
-f:执行一个sed脚本文件中的指令,例如: sed -f  scriptfile file(s)
 
-i:与-e的区别在于:当使用-e时,sed执行指令并不会修改原输入文件的内容,只会显示在bash中,而使用-i选项时,sed执行的指令会直接修改原输入文件。
 
-n:读取下一行到pattern space。
 
 
 
 
 
 
 
 
 
 

shell学习(2)- sed的更多相关文章

  1. 【Linux】shell学习之sed

    sed替换命令 使用该命令,可以将特定字符串或匹配的规则表达式用另一个字符串替换. sed 's/88/--/' filename 将filename每行第一次出现的88用字符串--替换,然后将该文件 ...

  2. Shell学习:grep, sed, awk命令的练习题

    http://www.cnblogs.com/chengmo/archive/2013/01/17/2865479.html 文件:datafileSteve Blenheim:238-923-736 ...

  3. Shell—学习之心得

    由于项目要招聘需要有经验shell开发人员(awk编程),而作为技术面试官(暂时)的我对shell编程不太熟:当然以前也写过一些shell脚本来满足项目的需求—备份环境,数据库(逻辑).假如只是针对a ...

  4. shell学习笔记

    shell学习笔记 .查看/etc/shells,看看有几个可用的Shell . 曾经用过的命令存在.bash_history中,但是~/.bash_history记录的是前一次登录前记录的所有指令, ...

  5. shell学习指南-阅读笔记

    shell学习指南真不是刚开始学习shell应该看得书,虽然其中讲了简单的linux命令,shell语法等,但是每章也有些深入和生僻地方,我想如果我刚学shell看到这样的地方一定会头疼的要死.或许也 ...

  6. Shell学习之结合正则表达式与通配符的使用(五)

    Shell学习之结合正则表达式与通配符的使用 目录 通配符 正则表达式与通配符 通配符 通配符的使用 正则表达式 正则表达式 正则表达式的使用 通配符 正则表达式与通配符 正则表达式用来在文件中匹配符 ...

  7. shell 学习笔记2-shell-test

    一.字符串测试表达式 前面一篇介绍:什么是shell,shell变量请参考: shell 学习笔记1-什么是shell,shell变量 1.字符串测试表达式参数 字符串需要用""引 ...

  8. shell学习总结之自定义函数

    shell学习总结之自定义函数 Myfun (){ echo -n "now i is $i " ! [ "$i" ] && exit ; ec ...

  9. SHELL学习笔记----IF条件判断,判断条件

    SHELL学习笔记----IF条件判断,判断条件 前言: 无论什么编程语言都离不开条件判断.SHELL也不例外.  if list then           do something here   ...

  10. 【转】shell学习笔记(一)——学习目的性、特殊字符、运算符等

    1 学习shell的目的性 写之前我们先来搞清楚为什么要学shell,学习要有目的性 shell简单.灵活.高效,特别适合处理一些系统管理方面的小问题 shell可以实现自动化管理,让系统管理员的工作 ...

随机推荐

  1. C++设计模式实现--策略(Strategy)模式

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/L_Andy/article/details/30489331 一. 举例说明 曾经做了一个程序,程序 ...

  2. Mongoose学习(2)

    1.Schema的扩展 UserSchema.methods.findUserName = function(cb){ return this.model('user').find({username ...

  3. github多用户提交错误Permission to repo denied to

    背景:同一台电脑的public key同时添加到了github的两个账户,导致user1的仓库没法正常提交. 解决办法:为两个账户分别配置ssh key,配置~/.ssh/config文件(windo ...

  4. Android JNI技术介绍【转】

    本文转载自:http://blog.csdn.net/yangwen123/article/details/8085833 JNI是JavaNative Interface 的缩写,通过JNI,Jav ...

  5. 牛逼的This使用

    今天看到一个很不错的this使用demo: package com.toov5.Reordering; class Message1{ private Channel channel; private ...

  6. ansible操作模块相关

    1. 查看模块可用参数命令 ansible-doc -s module_name

  7. 浅谈如何删除JSP编译后的空行

    当你在客户端用view source看JSP生成的代码时,会发现有很多空行,他们是由< %...% >后的回车换行而生成的,也就是说每一行由< %...% >包含的JSP代码到 ...

  8. 浏览器端JS导出EXCEL

    浏览器端JS导出EXCEL FileSaver.js 实现了在本身不支持 HTML5 W3C saveAs() FileSaver 接口的浏览器支持文件保存.FileSaver.js 在客户端保存文件 ...

  9. MAC 地址解析

    windows cmd 命令行通过 ipconfig /all:查看本地的 ip 地址以及 mac地址: MAC 地址的前三个字节代表厂商: 00:50:56/00:0c:29:vmware 公司 c ...

  10. SDOI2017 Round1 Day2 题解

    T2好厉害啊……AK不了啦……不过要是SCOI考这套题就好了240保底. BZOJ4819 新生舞会 模板题,分数规划+二分图最大权匹配. 费用流跑得过,可以不用KM. UPD:分数规划用迭代跑得飞快 ...