一、替换(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. 【智能无线小车系列十】通过USB摄像头实现网络监控功能

    如果仅有静态图像可能还不足以满足我们的需求,我们可能会需要用到实时的监控功能.这里介绍一款小应用:motion.motion的功能可强大了,不仅可以将监控的画面通过视频传输,实时展现,更为强大的是,m ...

  2. listview 使用图片三级缓存图片闪动

  3. 关于web页自动适配屏幕大小

    一.先了解下html5的viewport使用 随着高端手机(Andriod,Iphone,Ipod,WinPhone等)的盛行,移动互联应用开发也越来越受到人们的重视,用html5开发移动应用是最好的 ...

  4. Intellij IDEA 修改代码后自动编译更新

    Intellij IDEA 一些不为人知的技巧 问题描述: Intellij IDEA 调试修改时,改动页面和 java 文件后,无法立刻看到变化,需要手动重启服务. 问题原因: 在 IDEA tom ...

  5. Android View中滚动相关

    方法   scrollTo: (内容的左上角)达到某个地点 scrollBy: 根据当前位置,再移动多少 属性:   mScrollX, 以下是文档解释   The offset, in pixels ...

  6. double转int时精度不一致问题

    float和double类型的主要设计目的是为了科学计算和工程计算.它们执行二进制浮点运算,这是为了在广域数值范围上提供较为精确的快速近似计算而精心设计的.然而,它们没有提供完全精确的结果,所以不应该 ...

  7. codeforces 702A A. Maximum Increase(水题)

    题目链接: A. Maximum Increase time limit per test 1 second memory limit per test 256 megabytes input sta ...

  8. 多线程之:synchonized锁实现的原理<一>

    一:java同步的锁类型? --->目前在Java中存在两种锁机制:synchonized和Lock--->Lock接口及其实现类是JDK5增加的内容,其作者是大名鼎鼎的并发专家Doug ...

  9. AngularJS系统学习之Scope(作用域)

    本文出自:https://www.w3ctech.com/topic/1611 看完了没怎么懂,  也许是和别人 原文作者: Nicolas Bhttps://www.w3ctech.com/topi ...

  10. unity3D +php +数据库

    本文只是自己学习 摘录网上前人的学习资料,并非自己写的!!!!!!最近有项目需要用到数据库,网上有不少资料,但是整理后发现,那些资料对于完全没有PHP+MySQL经验的人来说还是很难理解,所以分享一下 ...