shell - sed的用法与实例介绍

Linux sed命令

Linux sed命令是利用script来处理文本文件。

sed可依照script的指令,来处理、编辑文本文件。

Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。

语法

sed [-hnV][-e<script>][-f<script文件>][文本文件]

参数说明

  • -e<script>或--expression=<script> 以选项中指定的script来处理输入的文本文件。
  • -f<script文件>或--file=<script文件> 以选项中指定的script文件来处理输入的文本文件。
  • -h或--help 显示帮助。
  • -n或--quiet或--silent 仅显示script处理后的结果。
  • -V或--version 显示版本信息。
  • -i就地编辑文件

动作说明

  • a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
  • c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
  • d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
  • i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
  • p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
  • s :取代,可以直接进行取代的工作!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g !

 

示例文档 - sed_test.txt:

Every 2.0s: cat sed_test.txt                                        Sun Mar 31 08:47:18 2019

This is one sed text.
I onem beautyful.
You onere not beautyful.
He is onelso not beautyful.
Hei Hei.
Are you loneugh?
No, you conen not do that.
Are you soned?
Oh, It is very good.

sed 替换

Sed 可替换文件中的字串、资料行、甚至资料区。其中,表示替换字串的指令中的函数参数为s;表示替换资料行、或资料区>的指令中的函数参数为c。上述情况以下面三个例子说明。

*行的替换
(1) sed -e '1c/#!/bin/more' file (把第一行替换成#!/bin/more)
    思考: 把第n行替换成just do it
    sed -e 'nc/just do it' file

(2) sed -e '1,10c/I can do it' file  (把1到10行替换成一行:I can do it)
    思考: 换成两行(I can do it! Let's start)
    sed -e '1,10c/I can do it!/nLet'"/'"'s start' file

*字符的替换
(3) sed -e 's/word1/& word2/' file (将每一行的word1单词替换成s参数最多与两个位置参数相结合,函数参数s中有两个特殊的符号:
    & : 代表pattern
    /n : 代表 pattern 中被第 n 个 /( 、/)(参照[附录 A]) 所括起来的字串。例如

    sed -e 's/w1/& w2/' file  # w1的地方输出 w1 w2
    sed -e  's//(test/) /(my/) /(car/)/[/2 /3 /1]/' file   #结果: [my car test]

*flag 参数举例
    sed -e 's/w1/& w2/g' file
    g : 代表替换所有匹配项目;这里,文件中所有字符串w1都会被替换成字串w1 w2
    sed -e 's/w1/& w2/10' file
    m(10) : 替换行内第m个符合的字串; 记住,是行内的第m个匹配的字串
    sed -e 's/w1/& w2/p' file
    p : 替换第一个和w1匹配的字符串为w1 w2,并输出到标准输出.
    sed -e 's/w1/& w2/w w2file' file
    w filename : 该参数会将替换过的内容写入到文件w2file并输出替换后的整个文件。注意w2file里写的只是替换过的行。    sed 'e 's/w1/& w2/' file
    这里的flag 为空, 这样就只是将第一个w1匹配的字符串替换成w1 w2而后面的不进行替换。

*位置参数应用举例
    sed -e '/machine/s/phi/beta/g' file
    将文件中含"machine"字串的资料行中的"phi"字串,替换成为"beta"字串
    sed -e '1,10 s/w1/& w2/g' file
    把1到10内的w1字符串替换成w1 w2字符串。
    sed -e '1,/else/ s/w1/& w2/g' file
    把1到字符串else内的w1字符串替换成w1 w2字符串。

其它位置参数的应用与前面的相同。


sed -i 's/beautyful/chou/' sed_test.txt: 把所有的 beautyful 替换为 not beautyful
格式:sed -i 's/被替换内容/替换的内容/' 文本文件

# -i 就地编辑文件

# s 取代,可以直接进行取代的工作

Every 2.0s: cat sed_test.txt                                        Sun Mar 31 08:48:41 2019

This is one sed text.
I onem chou.
You onere not chou.
He is onelso not chou.
Hei Hei.
Are you loneugh?
No, you conen not do that.
Are you soned?
Oh, It is very good.

sed多重指令的实现方式
(1)使用分号相隔: sed -i 's/chou/beautyful/;s/one/a/' sed_test.txt

This is a sed text.
Every 2.0s: cat sed_test.txt                                        Sun Mar 31 09:00:00 2019

This is a sed text.
I am beautyful.
You are not beautyful.
He is also not beautyful.
Hei Hei.
Are you laugh?
No, you can not do that.
Are you sad?
Oh, It is very good.

(2)在每个指令前放置 -e: sed -i -e 's/beautyful/chou/' -e 's/a/one/' sed_test.txt 

Every 2.0s: cat sed_test.txt                                        Sun Mar 31 09:01:20 2019

This is one sed text.
I onem chou.
You onere not chou.
He is onelso not chou.
Hei Hei.
Are you loneugh?
No, you conen not do that.
Are you soned?
Oh, It is very good.

(3)使用shell的分行指令功能

[root@centos7 shell]# sed -i '
> s/chou/beautyful/
> s/one/a/' sed_test.txt

结果:

This is a sed text.
Every 2.0s: cat sed_test.txt                                        Sun Mar 31 09:06:15 2019

This is a sed text.
I am beautyful.
You are not beautyful.
He is also not beautyful.
Hei Hei.
Are you laugh?
No, you can not do that.
Are you sad?
Oh, It is very good.

sed 脚本文件
当命令很少的时候,我们采取上述直接在命令行输入是可行的。但是当命令很长的时候,再输入这么长的脚本是不切合实际的。不过,我们可以通过创建脚本文件,吧要执行的命令书写在脚本文件中即可。
通过 -f 选项来指定命令行上的脚本文件的名字:sed -f scriptfile file ,scriptfile 即编写的脚本文件,file 即要操作的文本文件
编写脚本文件:vim sed_script
 

[root@centos7 shell]# cat sed_script 
s/beautyful/chou/
s/a/one/
s/good/very good/

执行:

sed -i -f sed_script sed_test.txt

 结果:

Every 2.0s: cat sed_test.txt                                        Sun Mar 31 09:15:49 2019

This is one sed text.
I onem chou.
You onere not chou.
He is onelso not chou.
Hei Hei.
Are you loneugh?
No, you conen not do that.
Are you soned?
Oh, It is very very good.

 

 sed默认输出是输出到终端,如果想把sed的输出重定向到另一个程序中,需要在后面指定一个I/O重定向符号。

sed -f scriptfile file > newfile

 

 Linux下批量替换多个文件中的字符串的简单方法。用sed命令可以批量替换多个文件中的字符串。

命令如下:

sed -i "s/原字符串/新字符串/g" `grep 原字符串 -rl 所在目录`

例如:我要把 charset=gb2312 替换为 charset=UTF-8,执行命令:

sed -i "s/charset=gb2312/charset=UTF-8/g" `grep charset=gb2312 -rl /www`

即可。

 

解释一下:

-i 表示inplace edit,就地修改文件

-r 表示搜索子目录

-l 表示输出匹配的文件名

这个命令组合很强大,要注意备份文件。

sed中y命令与s命令的区别

首先这两个命令都可以用作替换,但替换时还是有区别的

(1)y一般是行级别的替换,s一般是列级别替换(当然也可以转换成行级);

y:字元的替换
(1)sed -e 'y/abc../xyz../' filename
    把文件中的a字母替换成x, b替换成y, c替换成z。
(2) sed  -e 'y/abc/ABC' filename
    把小写的abc转换成大写的ABC

(2)s替换的是整体,y替换的是每一字母对应的单个字母

 

例:

1、sed 's/dog/cat/' data     把data中的所有行中的第一次出现dog的替换成cat,(注意:如果cat/后没有出现其他形式,则默认时第一次出现的位置)

2、sed '1,3y/abc/ABC/' data    把data中的第一行至第三行中的a替换成A,b替换成B,c替换成C

3、sed '2,${s/dog/cat/1}' data   把data中的第二行至最后一行的第一次出现dog的替换成cat

 

测试文档:

Every 2.0s: cat /mnt/sed/test_sed                                   Sun Mar 31 09:33:50 2019

1234567890

2345678901

3456789012

4567890123

执行:

sed -i 'y/1234567890/ABCDEFGHIJ/' test_sed

 

 结果:


Every 2.0s: cat /mnt/sed/test_sed                                   Sun Mar 31 09:35:13 2019

ABCDEFGHIJ

BCDEFGHIJA

CDEFGHIJAB

DEFGHIJABC

注意: 变换关系是按两个list的位置对应变换

print ‘=================================================================================’

print ‘=================================================================================’

删除:d命令

(1) sed -e '1d' inputfile (删除第一行)
    那么删除第x行呢?删除第x1,x2,x3行呢?
    sed -e 'xd' inputfile
    sed -e 'x1d' -e 'x2d' -e 'x3d' inputfile
    当然也许还有更好的办法。

(2) sed -e '1,3d' file (删除第一到第三行)
    思考:删除第n行到第m行?也就是
    sed -e 'n,md' file
    删除第一行到最后一行
    sed -e '1,$d' file     #$ 最后一行和一行的最后

(3) sed -e '/#/d' file  (删除含有'#'号的行)
    思考:删除含有字母xx的行
    sed -e '/xx/d' file
    思考: 删除除含有字符串xx的所有行
    sed -e '/xx/!d' file

(4) sed -e '/word1/, /word2/d' file  (删除从含有单词word1到含有单词word2的行)
    sed -e '10,/word1/d' file
    删除文件中从第10行到含有word1的行
    sed -e '/word1/,10/d' file
    和上面的匹配相反,删除从含有word1的行到第10行

(5) sed -e '/t.*t/d' file     (删除含有两个t的行)
    思考:删除含有指定正在表达式匹配的行。

删除文本的第二行:如下

sed -i '2d' test_sed

结果:

Every 2.0s: cat /mnt/sed/test_sed                                   Sun Mar 31 09:40:04 2019

ABCDEFGHIJ
BCDEFGHIJA

CDEFGHIJAB

DEFGHIJABC

删除文件最后一行:

sed -i '$d' test_sed

 结果:

Every 2.0s: cat /mnt/sed/test_sed                                   Sun Mar 31 09:42:09 2019

ABCDEFGHIJ
BCDEFGHIJA

CDEFGHIJAB

删除文件第二行到末尾所有行:

sed -i '2,$d' test_sed

结果:

Every 2.0s: cat /mnt/sed/test_sed                                   Sun Mar 31 09:44:22 2019

ABCDEFGHIJ

删除所有包含A的行:

sed -i '/A/'d test_sed

 结果: 

# 文件已经被清空

Every 2.0s: cat /mnt/sed/test_sed  

print ‘=================================================================================’

print ‘=================================================================================’

打印:p命令

基本格式:
    [address1,[address2]] p

    (1) sed -e '/then/ p' filename  #打印所有行并重复打印含有then 的行
    (2) sed -n '/then/ p' filename  #只打印含有then的行
    (3) sed -e '1,3 p' filename     # 打印所有行并重复1-3行
    (4) sed -n '1,3 p' filename     # 打印1-3行
    (5) sed -n '/if/,/fi/ p' filename #打印字符if和fi之间的内容

    p函数为sed的打印函数,在这里要注意-e 和-n 参数的区别。一般使用-n参数。

print ‘=================================================================================’

print ‘=================================================================================’

 

替换:s命令

Sed 可替换文件中的字串、资料行、甚至资料区。其中,表示替换字串的指令中的函数参数为s;表示替换资料行、或资料区>的指令中的函数参数为c。上述情况以下面三个例子说明。

*行的替换
(1) sed -e '1c/#!/bin/more' file (把第一行替换成#!/bin/more)
    思考: 把第n行替换成just do it
    sed -e 'nc/just do it' file

(2) sed -e '1,10c/I can do it' file  (把1到10行替换成一行:I can do it)
    思考: 换成两行(I can do it! Let's start)
    sed -e '1,10c/I can do it!/nLet'"/'"'s start' file

*字符的替换
(3) sed -e 's/word1/& word2/' file (将每一行的word1单词替换成s参数最多与两个位置参数相结合,函数参数s中有两个特殊的符号:
    & : 代表pattern
    /n : 代表 pattern 中被第 n 个 /( 、/)(参照[附录 A]) 所括起来的字串。例如

    sed -e 's/w1/& w2/' file  # w1的地方输出 w1 w2
    sed -e  's//(test/) /(my/) /(car/)/[/2 /3 /1]/' file   #结果: [my car test]

*flag 参数举例
    sed -e 's/w1/& w2/g' file
    g : 代表替换所有匹配项目;这里,文件中所有字符串w1都会被替换成字串w1 w2
    sed -e 's/w1/& w2/10' file
    m(10) : 替换行内第m个符合的字串; 记住,是行内的第m个匹配的字串
    sed -e 's/w1/& w2/p' file
    p : 替换第一个和w1匹配的字符串为w1 w2,并输出到标准输出.
    sed -e 's/w1/& w2/w w2file' file
    w filename : 该参数会将替换过的内容写入到文件w2file并输出替换后的整个文件。注意w2file里写的只是替换过的行。    sed 'e 's/w1/& w2/' file
    这里的flag 为空, 这样就只是将第一个w1匹配的字符串替换成w1 w2而后面的不进行替换。

*位置参数应用举例
    sed -e '/machine/s/phi/beta/g' file
    将文件中含"machine"字串的资料行中的"phi"字串,替换成为"beta"字串
    sed -e '1,10 s/w1/& w2/g' file
    把1到10内的w1字符串替换成w1 w2字符串。
    sed -e '1,/else/ s/w1/& w2/g' file
    把1到字符串else内的w1字符串替换成w1 w2字符串。

其它位置参数的应用与前面的相同。

 

在整行范围内把test替换为mytest。如果没有g标记,则只有每行第一个匹配的test被替换成mytest。

sed -i 's/test/mytest/' test_sed

 

 结果:

[root@centos7 sed]# sed 's/test/mytest/' test_sed 
this is a mytest and test.
this is a mytest and test.
this is a mytest and test.
this is a mytest and test.
this is a mytest and test.
this is a mytest and test.

 

加g标记:

sed 's/test/mytest/g' test_sed

结果:

[root@centos7 sed]# sed 's/test/mytest/g' test_sed 
this is a mytest and mytest.
this is a mytest and mytest.
this is a mytest and mytest.
this is a mytest and mytest.
this is a mytest and mytest.
this is a mytest and mytest.

 (-n)选项和p标志一起使用表示只打印那些发生替换的行。也就是说,如果某一行开头的test被替换成mytest,就打印它。

sed -n 's/^test/mytest/p' test_sed

 测试文档:

Every 2.0s: cat /mnt/sed/test_sed                                   Sun Mar 31 10:03:43 2019

this is a test and test.
this is a test and test.
this is a test and test.
this is a test and test.
this is a test and test.
this is a test and test.

&符号表示替换换字符串中被找到的部份。所有以this开头的行都会被替换成它自已加This,变成thisThis:如下

[root@centos7 sed]# sed 's/^this/&This/' test_sed 
thisThis is a test and test.
thisThis is a test and test.
thisThis is a test and test.
thisThis is a test and test.
thisThis is a test and test.
thisThis is a test and test.

an被标记为1,所有and会被替换成anand:如下

[root@centos7 sed]# sed 's/\(an\)d/\1and/' test_sed 
this is a test anand test.
this is a test anand test.
this is a test anand test.
this is a test anand test.
this is a test anand test.
this is a test anand test.

不论什么字符,紧跟着s命令的都被认为是新的分隔符,所以,“#”在这里是分隔符,代替了默认的“/”分隔符。表示把所有10替换成100。 

sed 's#10#100#g' expmple_file

所有在模板that和test所确定的范围内的行都被打印:如下

测试文档:

Every 2.0s: cat /mnt/sed/test_sed                                   Sun Mar 31 10:13:08 2019

this is a test and test.
this is a test and test.
this is a test and test.
this is a test and test.
this is a test and test.
this is a test and test.

that is not a test;

测试结果:

[root@centos7 sed]# sed -n '/that/, /test/p' test_sed 
that is not a test;

打印从第五行开始到第一个包含以test开始的行之间的所有行:如下

测试文档:


  1 this is a test and test.
  2 this is a test and test.
  3 this is a test and test.
  4 this is a test and test.
  5 that is a test and test.
  6 this is a test and test.
  7 
  8 that is not a test;
~                                                                                           
~                                                                                           
~                                                                                           
"/mnt/sed/test_sed" 8L, 171C  

测试结果:

[root@centos7 sed]# sed -n '5, /this/p' test_sed 
that is a test and test.
this is a test and test.

打印所有在模板that和that所确定的范围内的行 :如下

[root@centos7 sed]# sed -n '/that/, /that/p' test_sed 
that is a test and test.
this is a test and test.

that is not a test;

 

对于模板that和that之间的行,每行的末尾用字符串sed test替换:如下

[root@centos7 sed]# sed '/that/, /that/s/$/sed test/' test_sed 
this is a test and test.
this is a test and test.
this is a test and test.
this is a test and test.
that is a test and test.sed test
this is a test and test.sed test
sed test
that is not a test;sed test

 print ‘========================================================================================’

print ‘========================================================================================’

多点编辑:e命令

(-e)选项允许在同一行里执行多条命令。如例子所示,第一条命令删除1至3行,第二条命令用check替换test。命令的执行顺序对结果有影响。如果两个命令都是替换命令,那么第一个替换命令将影响第二个替换命令的结果。

测试文档:

  1 this is a test and test.
  2 this is a test and test.
  3 this is a test and test.
  4 this is a test and test.
  5 that is a test and test.
  6 this is a test and test.
  7 
  8 that is not a test;

 测试结果:

[root@centos7 sed]# sed -e '1,3d' -e 's/test/check/' test_sed 
this is a check and test.
that is a check and test.
this is a check and test.

that is not a check;

加g:

[root@centos7 sed]# sed -e '1,3d' -e 's/test/check/g' test_sed 
this is a check and check.
that is a check and check.
this is a check and check.

that is not a check;

删除包含not的行:

测试文档:

  1 this is a test and test.
  2 this is a test and test.
  3 this is a test and test.
  4 this is a test and test.
  5 that is a test and test.
  6 this is a test and test.
  7 
  8 that is not a test;

 测试结果:

[root@centos7 sed]# sed '/not/d' test_sed 
this is a test and test.
this is a test and test.
this is a test and test.
this is a test and test.
that is a test and test.
this is a test and test.

print ‘==========================================================================================’ 

print ‘==========================================================================================’ 

从文件读入: 

file里的内容被读进来,显示在与test匹配的行后面,如果匹配多行,则file的内容将显示在所有匹配行的下面。 

sed '/test/r file' example

测试文档test_sed:

  1 xiaoming age is 18;
  2 xiaohong age is 19;
  3 xiaohua age is 20;

测试文档test_sed02:

 1 age 18 is man;
 2 age 19 is woman;
 3 age 20 is woman;

测试结果:

[root@centos7 sed]# sed '/xiaoming/r test_sed02' test_sed
xiaoming age is 18;
age 18 is man;
age 19 is woman;
age 20 is woman;
xiaohong age is 19;
xiaohua age is 20;

测试结果02:

[root@centos7 sed]# sed '/age/r test_sed02' test_sed
xiaoming age is 18;
age 18 is man;
age 19 is woman;
age 20 is woman;
xiaohong age is 19;
age 18 is man;
age 19 is woman;
age 20 is woman;
xiaohua age is 20;
age 18 is man;
age 19 is woman;
age 20 is woman;

print ‘==========================================================================================’ 

print ‘==========================================================================================’ 

写入文件:w命令

在example中所有包含test的行都被写入file里。

sed -n '/test/w file' example

测试文档test_sed:

  1 xiaoming age is 18;
  2 xiaohong age is 19;
  3 xiaohua age is 20;

测试文档test_sed02:

  1 xiaoming age is 18;
  2 xiaohong age is 19;
  3 xiaohua age is 20;

执行:

sed -i '/age/w test_sed02' test_sed

测试结果:test_sed02:(上述执行语句将test_sed中包含age的行写入了test_sed02,test_sed02中的内容被覆盖)

  1 xiaoming age is 18;
  2 xiaohong age is 19;
  3 xiaohua age is 20;

print ‘======================================================================================’ 

print ‘======================================================================================’ 

追加命令:a命令

‘This is a example’被追加到以test开头的行后面,sed要求命令a后面有一个反斜杠。

sed '/^test/a\\This is a example' example

 测试文档:

  1 xiaoming age is 18;
  2 xiaohong age is 19;
  3 xiaohua age is 20;

测试结果:

[root@centos7 sed]# sed '/^xiaoming/a\\This is a example' test_sed
xiaoming age is 18;
This is a example
xiaohong age is 19;
xiaohua age is 20;

print ‘======================================================================================’ 

print ‘======================================================================================’ 

插入:i命令

基本格式:
    [address] i/ 插入内容 filename
 word2)
说明:
函数参数 s 表示替换(substitute)文件内字串。其指令格式如下 :
[address1[ ,address2]] s/pattern/replacemen/[flag]

    sed -e '/#/i/words' file      #在#字符的前面插入一行words

说明:
    这里的函数参数是i,它只能有一个地址参数。
    sed -e '1/i/words' file
    在第一行前加一行words
    cat "word" | sed -e '/$/.doc/g'   #输出word.doc
    在word后面加上后缀名,从而输出word.doc
    i 参数正好与a参数相反,它是插入到所给内容的前面.

a
    a参数的使用格式如下:
    [address] a/ <插入内容> filename

    sed -e '/unix/a/ haha' test.txt   #在含有unix的行后添加"haha"
    #输出结果为:
        unix
        haha

    另外: sed -e '1 a/ hh' test.txt  #在第一行后添加hh字符.

如果test被匹配,则把反斜杠后面的文本插入到匹配行的前面。

sed '/test/i\\new line-------------------------' example

测试文档:test_sed

  1 xiaoming age is 18;
  2 xiaohong age is 19;
  3 xiaohua age is 20;

 测试结果:

[root@centos7 sed]# sed '/^xiaoming/i\\--->this is a example' test_sed
--->this is a example
xiaoming age is 18;
xiaohong age is 19;
xiaohua age is 20;

 

print ‘======================================================================================’ 

print ‘======================================================================================’ 

读取资料下一行:n命令

基本格式:
    [address1[ ,address2]] n

    sed -n -e '/echo/n' -e 'p' temp
    表示输出文件,但如果一行含有字符串echo,则输出包含该字符串的下一行。
    sed -n -e 'n' -e 'p' filename
    输出文中的偶数行


如果test被匹配,则移动到匹配行的下一行,替换这一行的aa,变为bb,并打印该行,然后继续。

sed '/test/{ n; s/aa/bb/; }' example

测试文档:

  1 xiaoming age is 18;
  2 xiaohong age is 19;
  3 xiaohua age is 20;

测试结果:

[root@centos7 sed]# sed '/xiaohong/{n; s/20/18/;}' test_sed
xiaoming age is 18;
xiaohong age is 19;
xiaohua age is 18;

 

print ‘======================================================================================’ 

print ‘======================================================================================’ 

变形:y命令
把1–10行内所有abcde转变为大写,注意,正则表达式元字符不能使用这个命令。

sed '1,10y/abcde/ABCDE/' example

测试文档:test_sed

  1 xiaoming age is 18;
  2 xiaohong age is 19;
  3 xiaohua age is 20;

测试结果:将1-2行中的xmh替换为大写XMH

[root@centos7 sed]# sed '1,2y/xmh/XMH/' test_sed
XiaoMing age is 18;
XiaoHong age is 19;
xiaohua age is 20;

 测试结果02:注意小写的r在测试文档中1-2行不存在

[root@centos7 sed]# sed '1,2y/xmhr/XMHR/' test_sed
XiaoMing age is 18;
XiaoHong age is 19;
xiaohua age is 20;

 

print ‘======================================================================================’ 

print ‘======================================================================================’ 

退出:q命令
打印完前n行后,退出sed。

$ sed 'nq' example

测试文档:test_sed

  1 xiaoming age is 18;
  2 xiaohong age is 19;
  3 xiaohua age is 20;

 测试结果:

[root@centos7 sed]# sed '1q' test_sed
xiaoming age is 18;
[root@centos7 sed]# sed '2q' test_sed
xiaoming age is 18;
xiaohong age is 19;
[root@centos7 sed]# sed '3q' test_sed
xiaoming age is 18;
xiaohong age is 19;
xiaohua age is 20;

 

print ‘======================================================================================’ 

print ‘======================================================================================’ 

保持和获取:h命令和G命令
在sed处理文件的时候,每一行都被保存在一个叫模式空间的临时缓冲区中,除非行被删除或者输出被取消,否则所有被处理的行都将打印在屏幕上。接着模式空间被清空,并存入新的一行等待处理。在这个例子里,匹配test的行被找到后,将存入模式空间,h命令将其复制并存入一个称为保持缓存区的特殊缓冲区内。第二条语句的意思是,当到达最后一行后,G命令取出保持缓冲区的行,然后把它放回模式空间中,且追加到现在已经存在于模式空间中的行的末尾。在这个例子中就是追加到最后一行。简单来说,任何包含test的行都被复制并追加到该文件的末尾。

sed -e '/test/h' -e '$G' example

测试文档:

  1 xiaoming age is 18;
  2 xiaohong age is 19;
  3 xiaohua age is 20;

 测试结果:

# 将test_sed中匹配到xiaoming的行追加到最后一行
[root@centos7 sed]# sed -e '/xiaoming/h' -e '$G' test_sed
xiaoming age is 18;
xiaohong age is 19;
xiaohua age is 20;
xiaoming age is 18;

# 将test_sed中匹配到xiaoming的行追加到第2行的后面
[root@centos7 sed]# sed -e '/xiaoming/h' -e '2G' test_sed
xiaoming age is 18;
xiaohong age is 19;
xiaoming age is 18;
xiaohua age is 20;

 

print ‘======================================================================================’ 

print ‘======================================================================================’ 

保持和互换:h命令和x命令

互换模式空间和保持缓冲区的内容。也就是把包含test与check的行互换。

sed -e '/test/h' -e '/check/x' example

测试文档:test_sed


  1 xiaoming age is 18;
  2 xiaohong age is 19;
  3 xiaohua age is 20;

 测试结果:(注意:匹配到包含xiaoming的行将包含xiaohua的行给替换掉了)

[root@centos7 sed]# sed -e '/xiaoming/h' -e '/xiaohua/x' test_sed
xiaoming age is 18;
xiaohong age is 19;
xiaoming age is 18;

 

print ‘======================================================================================’ 

print ‘======================================================================================’ 

 反相执行命令 : !
    基本格式:
    [address1[ , address2]] ! 函数参数

    sed -e '/18/!d' filename
    删除除了不包含18的所有行。

测试文档:test_sed

  1 xiaoming age is 18;
  2 xiaohong age is 19;
  3 xiaohua age is 20;

测试结果:

# 删除包含18的所有行
[root@centos7 sed]# sed -e '/18/d' test_sed
xiaohong age is 19;
xiaohua age is 20;

# 删除不包含18的所有行
[root@centos7 sed]# sed -e '/18/!d' test_sed
xiaoming age is 18;

print ‘======================================================================================’ 

print ‘======================================================================================’ 

改变文件中的资料: c
    基本格式:
    [address1[ ,address2]]c/ filename
    函数参数 c 紧接着 "/" 字元用来表示此行结束 , 使用者所输入的资料必须从下一行输入。如果资料超过一行 , 则须在>每行的结尾加入"/"

    sed -e '/zhengxh/c hhhh' filename
    表示把含有字符串zhengxh的行,该成hhhh。

测试文档:

  1 xiaoming age is 18;
  2 xiaohong age is 19;
  3 xiaohua age is 20;

测试结果:

 

# 将包含小明的行,整行替换为mingming
[root@centos7 sed]# sed '/xiaoming/c mingming' test_sed
mingming
xiaohong age is 19;
xiaohua age is 20;

 

print ‘======================================================================================’ 

print ‘======================================================================================’ 

 

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值