一、替换(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. machine learning for hacker记录(1) R与机器学习

    开篇:首先这本书的名字很霸气,全书内容讲的是R语言在机器学习上面的应用,一些基本的分类算法(tree,SVM,NB),回归算法,智能优化算法,维度约减等,机器学习领域已经有很多成熟的R工具箱,毕竟这个 ...

  2. Java其实不支持垃圾回收

    Java其实不支持垃圾回收.如果真的支持的话,大多数Java程序在运行的一开始就应该把程序本身删除,因为这些程序本身就是垃圾.   // TODO: This is a 分割线. Please no ...

  3. 2018年东北农业大学春季校赛 B wyh的矩阵【规律】

    题目链接 https://www.nowcoder.com/acm/contest/93/B 思路 先加入 中间的那行 和中间的那列 再减去 最中间那个数 因为它 加了两次 然后逐行往下加 会发现是一 ...

  4. RabbitMQ的介绍与spring整合

    本文主要讲述的是个人参考官网及其他前辈博客,对RabbitMQ的一些理解与spring整个RabbitMQ. 一.RabbitMQ的介绍 1.1.什么是RabbitMQ RabbitMQ是一个由erl ...

  5. oracle添加表注释和表字段注释

    创建Oracle数据库表时加上注释 CREATE TABLE t1( id  varchar2(32) primary key,name VARCHAR2(8) NOT NULL, age numbe ...

  6. Android Jni(Java Native Interface)笔记

    首先记录一个问题,关于如何用javah生成头文件. 为什么要生成头文件?在含有 static{ System.loadLibrary("hellojni"); } 这样代码的类下面 ...

  7. ORA-12547: TNS:lost contact

    碰到这个ORA-12547: TNS:lost contact的问题,翻了很多资料和METALINK,总结了一下原因: 1 是由于rpm包没有安装,对于我们的生产环境,此包是安装的. admin@p1 ...

  8. 【USACO】Optimal Milking

    题目链接 :        [POJ]点击打开链接        [caioj]点击打开链接 算法 : 1:跑一遍弗洛伊德,求出点与点之间的最短路径 2:二分答案,二分”最大值最小“ 3.1:建边,将 ...

  9. 单次目标检测器-YOLO简介

    YOLO 在卷积层之后使用了 DarkNet 来做特征检测. 然而,它并没有使用多尺度特征图来做独立的检测.相反,它将特征图部分平滑化,并将其和另一个较低分辨率的特征图拼接.例如,YOLO 将一个 2 ...

  10. HttpClient入门教程

    HttpClient使用详解与实战一:https://www.jianshu.com/p/375be5929bed