Linux Sed命令学习笔记

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux Sed命令学习笔记相关的知识,希望对你有一定的参考价值。

1 功能说明

    sed是一种在线编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出或者加入i参数。Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。

                            技术分享

   小结:sed的功能是,对字符串进行增加、删除、改变、查找,即增删改查!

2 语法格式

  sed [optian] [sed-command] [input-file]

 

  • 注意sed和后面的选项之间至少有一个空格。

  • 为了避免混淆,称呼sedsed软件。sed-commands(sed命令)sed软件内置的一些命令选项,为了和前面的options(选项)区分,故称为sed命令。

  • sed-commands既可以是单个sed命令,也可以是多个sed命令组合。

  • input-file(输入文件)是可选项,sed还能够从标准输入如管道获取输入。

3 常用选项

测试文本内容为:

[[email protected] ~]# cat oldboy.txt
I am oldboy teacher!
I teach linux.
 
I like badminton ball ,billiard ball and chinesechess!
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 49000448.
 
not 4900000448.
my god ,i am not oldbey,but OLDBOY!
gd
good
gooood
[[email protected] ~]#

3.1 取消标准输出

    -n:表示取消标准输出(默认情况下sed替换后会把文件所有内容包括更改过的内容一起输出到屏幕上,我们使用-n后,让它只输出匹配表达式的字符串,一般和p连用,p表示print,打印表达式结果)。

[[email protected] ~]# sed ‘/old/p‘ oldboy.txt   
I am oldboy teacher!
I am oldboy teacher!
I teach linux.
 
I like badminton ball ,billiard ball and chinesechess!
my blog is http://oldboy.blog.51cto.com
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 49000448.
 
not4900000448.
my god ,iam not oldbey,but OLDBOY!
my god ,iam not oldbey,but OLDBOY!
gd
good
gooood
[[email protected]~]# sed  -n ‘/old/p‘ oldboy.txt
I amoldboy teacher!
my blogis http://oldboy.blog.51cto.com
my god ,iam not oldbey,but OLDBOY!
[[email protected]~]#     //-n和p连用后表示只打印符合表达式规则的行


3.2 支持扩展正则表达式

   -r:参数用来支持扩展正则表达式,比如{}符号,如果不加-r参数需要使用\来进行转意。

[[email protected] ~]# sed -n ‘/o\{1,2\}/p‘ oldboy.txt
I am oldboy teacher!
I like badminton ball ,billiard ball and chinesechess!
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
not 4900000448.
my god ,i am not oldbey,but OLDBOY!
good
gooood
[[email protected] ~]# sed -nr ‘/o{1,2}/p‘ oldboy.txt   
I am oldboy teacher!
I like badminton ball ,billiard ball and chinesechess!
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
not 4900000448.
my god ,i am not oldbey,but OLDBOY!
good
gooood
[[email protected] ~]# //表达式含义:o最少重复1次,最多重复2次


3.3 直接修改文件内容

   -i:表示直接对目标文件进行操作(增删改查),加上这个参数之后,不会在屏幕上输出信息,会直接对源文件进行更改(慎用)

[[email protected] ~]# sed -i ‘/^$/d‘ oldboy.txt  //直接删除文件内的空行,并没有打印。
[[email protected] ~]# cat oldboy.txt        
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinesechess!
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 49000448.
not 4900000448.
my god ,i am not oldbey,but OLDBOY!
gd
good
gooood
[[email protected] ~]#

4 sed-command

测试文本内容为:

[[email protected] ~]# cat study.txt
1,I am a Linux student
2,I am studying Linux
3,I like movice
4,I am super man
5,I like computer games
[[email protected] ~]#

4.1 增加

  -i:表示插入,插入到目标行之前。

[[email protected] ~]# sed ‘2i she is a girl‘ study.txt
1,I am a Linux student
she is a girl
2,I am studying Linux
3,I like movice
4,I am super man
5,I like computer games
[[email protected] ~]#   //2i表示在第二行插入,不加行号表示在每一行之前插入


 

   -a:表示在目标行之后添加

[[email protected] ~]# sed  ‘3a he is a boy‘ study.txt 
1,I am a Linux student
2,I am studying Linux
3,I like movice
he is a boy
4,I am super man
5,I like computer games
[[email protected] ~]#

 

  多行增加

方法一:利用\n,表示换行符

[[email protected] ~]# sed ‘2i 1\n2\n3‘ study.txt
1,I am a Linux student
1
2
3
2,I am studying Linux
3,I like movice
4,I am super man
5,I like computer games
[[email protected] ~]#

 

方法二:利用shell特性(\回车)表示接着下一行。

[[email protected] ~]# sed ‘2i 1> 2> 3 ‘ study.txt
1,I am a Linux student
1
2
3
2,I am studying Linux
3,I like movice
4,I am super man
5,I like computer games
[[email protected] ~]#

4.1.1 限定区域添加

sed软件可以对单行或多行进行处理。如果在sed命令前面不指定地址范围,那么默认会匹配所有行。

用法:

n1[,n2]{sed-commands}

注:地址用逗号分隔的,n1,n2可以用数字、正则表达式、或二者的组合表示。

例子:

 n{sed-commands}        对第n行操作。

[[email protected] ~]# sed ‘5c she is a girl ‘ study.txt
1,I am a Linux student
2,I am studying Linux
3,I like movice
4,I am super man
she is a girl
[[email protected] ~]#     //表示针对第武行进行替换,替换为she isa girl

n,m{sed-commands}     nm行操作,包括第n,m行。

[[email protected] ~]# sed ‘2,4c she is a girl ‘ study.txt
1,I am a Linux student
she is a girl
5,I like computer games
[[email protected] ~]#     //表示把2到4行替换成she is a girl。

n+m{sed-commands}   nn+m行操作,包括第n,m

[[email protected] ~]# sed ‘2,3d‘ study.txt
1,I am a Linux student
4,I am super man
5,I like computer games
[[email protected] ~]#         //表示2到3行删除


 

 1~2{sed-commands}       1,3,5,7,……行操作

[[email protected] ~]# sed ‘2~2d‘ study.txt 
1,I am a Linux student
3,I like movice
5,I like computer games
[[email protected] ~]#         //表示对2,4,6,8等偶数行进行操作

n${sed-commands}     n到最后一行($代表最后一行)操作,包括第n

[[email protected] ~]# sed ‘2,$d‘ study.txt
1,I am a Linux student
[[email protected] ~]#         //删除2行到$(尾行)

   

/Linux/{sed-commands}         对匹配oldboy的行操作

[[email protected] ~]# sed ‘/Linux/d‘ study.txt
3,I like movice
4,I am super man
5,I like computer games
[[email protected] ~]#     //表示把包涵Linux的行删掉


   

/Linux/,/movice/{sed-commands}  对匹配oldboy的行到匹配Alex的行操作

[[email protected] ~]# sed ‘/Linux/,/movice/d‘study.txt 
4,I am super man
5,I like computer games
[[email protected] ~]#         //表示把第一个包涵Linux到第一个包涵movice的行删掉


   

/movice/,${sed-commands}       对匹配oldboy的行到最后一行操作

[[email protected] ~]# sed ‘/movice/,$d‘ study.txt
1,I am a Linux student
2,I am studying Linux
[[email protected] ~]#         //表示从movice到最后一行删掉


 

/games/,10{sed-commands}      对匹配oldboy的行到第10行操作,注意:如果前10行没有匹配到oldboysed软件会显示10行以后的匹配oldboy的所有行,如果有。

[[email protected] ~]# sed ‘/games/,4d‘ study.txt   
1,I am a Linux student
2,I am studying Linux
3,I like movice
4,I am super man
[[email protected] ~]#

 

1,/am/{sed-commands}         对第1行到匹配Alex的行操作

[[email protected] ~]# sed ‘1,/am/d‘ study.txt
3,I like movice
4,I am super man
5,I like computer games
[[email protected] ~]#      //表示从1行到第一个匹配到am的行删除


   

/student/,+2{sed-commands}      对匹配oldboy的行到其后的2行操作

[[email protected] ~]# sed ‘/student/,+2d‘ study.txt
4,I am super man
5,I like computer games
[[email protected] ~]#         //表示从匹配student的第一项到它的后两行进行删除


4.2 删除

  -d:表示删除,可以直接删除行,可以删除包涵关键字的行

[[email protected] ~]# sed ‘2d‘ study.txt
1,I am a Linux student
3,I like movice
4,I am super man
5,I like computer games
games                         //删除第2行
[[email protected] ~]# sed ‘/am/d‘ study.txt
3,I like movice
[[email protected] ~]#                //删除包涵am的行


4.3 查找

    p表示打印,把符合我们表达式的项目打印出来,由于sed默认输出所有,再使用拍的话会打印两遍,所以一般和-n参数连用,表示只打印匹配到的行。

[[email protected] ~]# sed ‘/am/p‘ study.txt  //由于没有-n所以会把匹配到的行和默认输出一起打印
1,I am a Linux student
1,I am a Linux student
2,I am studying Linux
2,I am studying Linux
3,I like movice
4,I am super man
4,I am super man
5,I like computer games
5,I like computer games
games
games
[[email protected] ~]# sed -n ‘/am/p‘ study.txt    //-n取消默认输出,p打印,所以只显示匹配项
1,I am a Linux student
2,I am studying Linux
4,I am super man
5,I like computer games
games
[[email protected] ~]#
[[email protected] ~]# sed -n ‘2p‘ study.txt
2,I am studying Linux
[[email protected] ~]#          //只显示第二行


4.4

-c:按行替换,表示一次替换一整行的内容。

[[email protected] ~]# sed ‘1c she is a girl‘ study.txt
she is a girl
2,I am studying Linux
3,I like movice
4,I am super man
5,I like computer games
games
[[email protected] ~]#       //表示把第1行,替换成she is a girl

 

4.4.1 文本替换

    s:表示全局查找某个关键字,并替换每行第一个匹配项。

    g:表示全局替换,不属于sed命令,一般搭配s使用。

        

格式:

     sed  ‘s#1#2#g’

  • #:表示定界符,可以是/@、或者其他成对符号。

  • 1:表示要替换的源文件中的字符串(可以是正则表达式)。

  • 2:表示要替换成的文件,必须是给定的字符串。

 

文本替换实例:

[[email protected]~]# !ca
cat study.txt
1,I am a Linux student
2,I am studying Linux
3,I like movice
4,I am super man
5,I like computer games
[[email protected] ~]# sed ‘1s#a#b#‘ study.txt     //把第一个a替换为b
1,I bm a Linux student
2,I am studying Linux
3,I like movice
4,I am super man
5,I like computer games
[[email protected] ~]#
[[email protected] ~]# sed ‘1s#a#b#g‘ study.txt  //把第一行所有a替换为b
1,I bm b Linux student
2,I am studying Linux
3,I like movice
4,I am super man
5,I like computer games
[[email protected] ~]#

变量替换实例:

[[email protected] ~]# n=a        //设定变量n的值为a
[[email protected] ~]# m=c       //设定变量m的值为c
[[email protected] ~]# echo $n    //验证变量n的值
a
[[email protected] ~]# echo $m   //验证变量b的值
c
[[email protected] ~]# sed ‘1s#$n#$m#g‘ study.txt  //由于’’单引号,不解释内部变量值所以没有替换
1,I am a Linux student
2,I am studying Linux
3,I like movice
4,I am super man
5,I like computer games
[[email protected] ~]# sed "1s#$n#$m#g"study.txt  // “” 双引号表示引用内部的变量,所以替换了
1,I cm c Linux student
2,I am studying Linux
3,I like movice
4,I am super man
5,I like computer games
[[email protected] ~]# //表示可以利用变量的值来对字符串进行过滤或替换(必须用””双引号)


4.4.2 后向引用

    sed软件的( )的功能可以记住正则表达式的一部分,其中,\1为第一个记住的模式即第一个小括号中的匹配内容,\2第二记住的模式,即第二个小括号中的匹配内容,sed最多可以记住9个。这种在后面调用的方式,叫做后向引用。

格式:

     sed ‘s#()()#\1\2#g’

  • 第一个()表示第一项,第二个()表示第二项,一次类推,最多九个,不想匹配的可以写在括号后面。

  • \1表示引用前面第一个()括号内的内容,\2表示引用前面第二个()括号内的内容。

实例:

获取ip地址信息

[[email protected] ~]# ifconfig eth0
eth0     Link encap:Ethernet  HWaddr00:0C:29:E4:83:06 
         inet addr:10.0.0.8 Bcast:10.0.0.255 Mask:255.255.255.0
         inet6 addr: fe80::20c:29ff:fee4:8306/64 Scope:Link
          UPBROADCAST RUNNING MULTICAST MTU:1500  Metric:1
          RXpackets:981 errors:0 dropped:0 overruns:0 frame:0
          TXpackets:562 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RXbytes:85039 (83.0 KiB)  TX bytes:62853(61.3 KiB)
[[email protected] ~]# ifconfig eth0 | sed -nr‘2s#^.*r:(.*).?B.*$#\1#gp‘
10.0.0.8 
[[email protected] ~]#
// ^.*r: 表示以任意字符开头到r:为止。
//(.*) 表示任意字符,这里就是我们要取得ip信息。
//.?B.*$ ,.?表示匹配任意两个字符,B.*$表示从B到结尾。


特殊符号的使用:

批量进行文件后缀名更改

[[email protected] lixin]# ls
LinuxAdministartor10.bak  LinuxAdministartor2.bak  LinuxAdministartor4.bak  LinuxAdministartor6.bak  LinuxAdministartor8.bak
LinuxAdministartor1.bak   LinuxAdministartor3.bak  LinuxAdministartor5.bak  LinuxAdministartor7.bak  LinuxAdministartor9.bak
[[email protected] lixin]# ls | sed -nr‘s#(^L.*[0-9])(.*$)#mv & \1.txt#gp‘
mv LinuxAdministartor10.bakLinuxAdministartor10.txt
mv LinuxAdministartor1.bak LinuxAdministartor1.txt
mv LinuxAdministartor2.bak LinuxAdministartor2.txt
mv LinuxAdministartor3.bak LinuxAdministartor3.txt
mv LinuxAdministartor4.bak LinuxAdministartor4.txt
mv LinuxAdministartor5.bak LinuxAdministartor5.txt
mv LinuxAdministartor6.bak LinuxAdministartor6.txt
mv LinuxAdministartor7.bak LinuxAdministartor7.txt
mv LinuxAdministartor8.bak LinuxAdministartor8.txt
mv LinuxAdministartor9.bak LinuxAdministartor9.txt
[[email protected] lixin]# ls | sed -nr‘s#(^L.*[0-9])(.*$)#mv & \1.txt#gp‘ | bash
[[email protected] lixin]# ls
LinuxAdministartor10.txt  LinuxAdministartor2.txt  LinuxAdministartor4.txt  LinuxAdministartor6.txt  LinuxAdministartor8.txt
LinuxAdministartor1.txt   LinuxAdministartor3.txt  LinuxAdministartor5.txt  LinuxAdministartor7.txt  LinuxAdministartor9.txt
[[email protected] lixin]#
// &符号表示前面两个分隔符中间的所有内容
//shell的命令都是bash在执行,所以把命令拼出来后直接交给bash处理即可



本文出自 “你的黑夜” 博客,请务必保留此出处http://lixin15.blog.51cto.com/3845983/1759779

以上是关于Linux Sed命令学习笔记的主要内容,如果未能解决你的问题,请参考以下文章

linux基础学习-7.5-基础命令

Linux学习笔记(二十七)sed

Linux学习笔记

Linux学习笔记2(sed命令)

2018-1-16 Linux学习笔记[重要]

Linux Shell学习-sed命令详解