文档章节

sed命令

Vincent-Duan
 Vincent-Duan
发布于 2016/10/12 19:53
字数 1931
阅读 10
收藏 0

sed命令的语法格式:

sed的命令格式: sed [option] 'sed command'filename

sed的脚本格式:sed [option] -f 'sed script'filename

sed命令的选项(option):

-n :只打印模式匹配的行

-e :直接在命令行模式上进行sed动作编辑,此为默认选项

-f :将sed的动作写在一个文件内,用–f filename 执行filename内的sed动作

-r :支持扩展表达式

-i :直接修改文件内容

 

测试文件sedtest.txt,内容为:

[root@localhost sed]# cat sedtest.txt 
my cat's name is betty
   it's a cat;
This is your dog
   its name is Lokia;
That is your cat
   it is a gitl
[root@localhost sed]# 

打印:p命令:

  • 打印sedtest.txt中包含"This"的行。

默认情况sed把所有行都打印到屏幕,如果某行匹配到模式,则把该行另外再打印一遍。例如:

[root@localhost sed]# sed  '/This/p' sedtest.txt 
my cat's name is betty
   it's a cat;
This is your dog
This is your dog
   its name is Lokia;
That is your cat
   it is a gitl
[root@localhost sed]# 
  •     和上面一样,只是去掉了sed的默认行为,只会打印匹配的行
[root@localhost sed]# sed -n '/This/p' sedtest.txt 
This is your dog
[root@localhost sed]# 

打印第10行到20行,sed -n '10,20p' sedtest.txt 

打印从'abc'行到'def'行,sed -n '/abc/,/def/p' sedtest.txt

间隔行:步进2行,sed -n '1~2p' sedtest.txt

删除:d命令:(不会修改源文件,-i可以修改原文件)

  • 删除从第3行到最后一行的内容。
[root@localhost sed]# sed '3,$d' sedtest.txt 
my cat's name is betty
   it's a cat;
[root@localhost sed]# 
  • 删除最后一行的内容
[root@localhost sed]# sed '$d' sedtest.txt 
my cat's name is betty
   it's a cat;
This is your dog
   its name is Lokia;
That is your cat
[root@localhost sed]# 
  • 删除包含“is”的行
[root@localhost sed]# sed '/is/d' sedtest.txt 
   it's a cat;
[root@localhost sed]# 

替换:s命令(默认不会修改原文件,"-i"可以修改原文件)

  • 把行内的所有"it"替换成"IT",如果没有g,则只替换行内的第一个it
[root@localhost sed]# sed 's/it/IT/' sedtest.txt 
my cat's name is betty
   IT's a cat,it has a tail;
This is your dog
   ITs name is Lokia;
That is your cat
   IT is a gitl
[root@localhost sed]# 
  • /g 表示全部替换
[root@localhost sed]# sed 's/it/IT/g' sedtest.txt 
my cat's name is betty
   IT's a cat,IT has a tail;
This is your dog
   ITs name is Lokia;
That is your cat
   IT is a gITl
[root@localhost sed]# 
  • -n  /p 只打印发生替换的那些行 
[root@localhost sed]# sed  -n 's/it/It/gp' sedtest.txt 
   It's a cat,It has a tail;
   Its name is Lokia;
   It is a gItl
[root@localhost sed]# 
  • sed  ‘s/it/&123/’ sedtest.txt :在行内第一个出现的的it后面添加123(&表示匹配的内容)
[root@localhost sed]# sed  's/it/&123/' sedtest.txt 
my cat's name is betty
   it123's a cat,it has a tail;
This is your dog
   it123s name is Lokia;
That is your cat
   it123 is a gitl
[root@localhost sed]# 
  • sed  's/it/&123/g' sedtest.txt:在所有的it后面添加123
[root@localhost sed]# sed  's/it/&123/g' sedtest.txt 
my cat's name is betty
   it123's a cat,it123 has a tail;
This is your dog
   it123s name is Lokia;
That is your cat
   it123 is a git123l
[root@localhost sed]# 
  • sed -n 's/it/&123/gp' sedtest.txt :在所有it后面添加123,打印受影响的行数
[root@localhost sed]# sed -n 's/it/&123/gp' sedtest.txt 
   it123's a cat,it123 has a tail;
   it123s name is Lokia;
   it123 is a git123l
[root@localhost sed]# 
  • sed 's#it#abc#g' sedtest.txt :把所有的it替换成abc,跟在替换s后面的字符就是查找串和替换串之间的分割字符,本例中试#
[root@localhost sed]# sed 's#it#abc#g' sedtest.txt 
my cat's name is betty
   abc's a cat,abc has a tail;
This is your dog
   abcs name is Lokia;
That is your cat
   abc is a gabcl
[root@localhost sed]# 

指定行的范围:逗号

  • sed -n '/dog/,/cat/p' sedtest.txt:打印模式dog到cat的行
[root@localhost sed]# sed -n '/dog/,/cat/p' sedtest.txt 
This is your dog
   its name is Lokia;
That is your cat
[root@localhost sed]# 
  • sed -n '2,/Lokia/p' sedtest.txt :打印从第2行到包含Lokia行之间的行
[root@localhost sed]# sed -n '2,/Lokia/p' sedtest.txt 
   it's a cat,it has a tail;
This is your dog
   its name is Lokia;
[root@localhost sed]# 
  • sed '/dog/,/cat/s/is/IS/g' sedtest.txt :修改从模式dog到模式cat之间的行,把is替换成IS
[root@localhost sed]# sed '/dog/,/cat/s/is/IS/g' sedtest.txt 
my cat's name is betty
   it's a cat,it has a tail;
ThIS IS your dog
   its name IS Lokia;
That IS your cat
   it is a gitl
[root@localhost sed]# 

多重编辑-e

  • sed -e '1,3d' -e 's/is/IS/g' sedtest.txt:删除1-3行,然后把其余行的is替换成IS
[root@localhost sed]# sed -e '1,3d' -e 's/is/IS/g' sedtest.txt 
   its name IS Lokia;
That IS your cat
   it IS a gitl
[root@localhost sed]# 

读文件:r命令

  •    sed '/it/r sedtest2.txt' sedtest.txt:在匹配'it'的行的下一行开始加入文件sedtest.txt的内容
vincent@vincent-duan:~/temp$ sed '/it/r sedtest2.txt' ./sedtest.txt 
my cat's name is betty
   it's a cat;
this is sedtest2.txt
This is your dog
   its name is Lokia;
this is sedtest2.txt
That is your cat
   it is a gitl
this is sedtest2.txt
vincent@vincent-duan:~/temp$ 

写文件:w命令

  • sed '/it/w sedtest2.txt' ./sedtest.txt :在包含‘it’的行写入sedtest2.txt
vincent@vincent-duan:~/temp$ cat sedtest2.txt 
hello world
vincent@vincent-duan:~/temp$ sed '/it/w sedtest2.txt' ./sedtest.txt 
my cat's name is betty
   it's a cat;
This is your dog
   its name is Lokia;
That is your cat
   it is a gitl
vincent@vincent-duan:~/temp$ cat sedtest2.txt 
   it's a cat;
   its name is Lokia;
   it is a gitl
vincent@vincent-duan:~/temp$ 

追加:a命令  

  • sed '/it/a\def' sedtest.txt :在包含‘it’行后新起一行,写入def
vincent@vincent-duan:~/temp$ sed '/it/a\def' sedtest.txt 
my cat's name is betty
   it's a cat;
def
This is your dog
   its name is Lokia;
def
That is your cat
   it is a gitl
def
vincent@vincent-duan:~/temp$ 
  •     在第5行后添加一行:sed '5a=====' sedtest.txt

插入:i命令  

  • sed '/it/i\def' sedtest.txt :在包含‘it’行前插入一行,写入def
vincent@vincent-duan:~/temp$ sed '/it/i\def' sedtest.txt 
my cat's name is betty
def
   it's a cat;
This is your dog
def
   its name is Lokia;
That is your cat
def
   it is a gitl
vincent@vincent-duan:~/temp$ 

修改:c命令   

  • sed '/This/c\THIS' sedtest.txt :将This替换成THIS,不会修改原文件(-i可以实现修改原文件)
vincent@vincent-duan:~/temp$ sed '/This/c\THIS' sedtest.txt 
my cat's name is betty
   it's a cat;
THIS
   its name is Lokia;
That is your cat
   it is a gitl
vincent@vincent-duan:~/temp$ 

读取下一行:n命令  

  • sed '/Lokia/{n;s/That/THAT/g;}' sedtest.txt:读取包含Lokia的行的下一行,替换That为THAT
vincent@vincent-duan:~/temp$ sed '/Lokia/{n;s/That/THAT/g;}' sedtest.txt 
my cat's name is betty
   it's a cat;
This
   its name is Lokia;
THAT is your cat
   it is a gitl
vincent@vincent-duan:

转换:y命令  

  • sed 'y/sl/SL/' sedtest.txt :将sedtest.txt中的s替换成S,l替换成L
vincent@vincent-duan:~/temp$ sed 'y/sl/SL/' sedtest.txt 
my cat'S name iS betty
   it'S a cat;
ThiS iS your dog
   itS name iS Lokia;
That iS your cat
   it iS a gitL
vincent@vincent-duan:~/temp$ 

退出:q命令

  • sed '/it/{n;s/This/THIS/;q}' sedtest.txt:将sedtest.txt中包含it的下一行,将This替换成THIS,然后退出
vincent@vincent-duan:~/temp$ sed '/it/{n;s/This/THIS/g;q}' sedtest.txt 
my cat's name is betty
   it's a cat;
THIS is your dog
vincent@vincent-duan:~/temp$

 

暂存和取用:h命令(把模式行存储到暂存缓冲区)和g(取出暂存缓冲区的行并覆盖模式缓冲区)G(取出临时缓冲区的行)命令 

h和g是复制行为(覆盖),H和G表示追加。

 sed -e '/is/h' -e '$G' sedtest.txt:包含abc的行通过h命令保存到暂存缓冲区,在第二条命令汇中,sed读到最后一行$时,G命令从暂存缓冲区中读取一行,追加到模式缓冲区的后面。即所有包含abc的行的最后一行被复制到文件末尾。

vincent@vincent-duan:~/temp$ sed -e '/is/h' -e '$G' sedtest.txt 
my cat's name is betty
   it's a cat;
This is your dog
   its name is Lokia;
That is your cat
   it is a gitl
   it is a gitl
vincent@vincent-duan:~/temp$ 

{}

sed '{20,30d;s/false/true/g}' file.txt 先将20行到30行删除,然后将flase替换成true

() 

\( \) :替换某种(部分)字符串(\1,\2)

删除每行第一个tab

sed -i 's/^[\t]//' file

替换{"username":"vincent","age":"25"}删除"username":"vincent"

 sed -i 's/"username":".*"//g' ./file.txt

删除每行第一个:数字+\t

sed 's/[0-9]*\t/' ./file.txt

删除第一行到最后一行数据:

sed -i "1,\$d" ./file.txt

 

 

© 著作权归作者所有

Vincent-Duan

Vincent-Duan

粉丝 33
博文 430
码字总数 237992
作品 0
海淀
私信 提问

暂无文章

uni app 零基础小白到项目实战

$emit 子组件传给父组件$ref 父组件操作子组件 公用模板 uni-app全局变量的几种实现方法 const websiteUrl = 'http'const now = Date.now || function() { return new Date().getTime......

达达前端小酒馆
31分钟前
7
0
Tomcat是如何实现异步Servlet的

前言 通过我之前的Tomcat系列文章,相信看我博客的同学对Tomcat应该有一个比较清晰的了解了,在前几篇博客我们讨论了Tomcat在SpringBoot框架中是如何启动的,讨论了Tomcat的内部组件是如何设...

木木匠
55分钟前
29
0
mysql中间件分享(Mysql-prxoy,Atlas,DBProxy,Amoeba,cobar,TDDL)

hello 各位小伙伴大家好,我是小栈君,这期我们分享关于mysql中间件的研究,也就是数据层的读写分离和负载均衡,希望能够在实际的应用中能够帮助到各位小伙伴。 下期我们将继续分享go语言的系...

IT干货栈
今天
10
0
OSChina 周一乱弹 —— 人生,还真是到处是意外

Osc乱弹歌单(2019)请戳(这里) 【今日歌曲】 @这次装个文艺青年吧 :#今日歌曲推荐# 分享lil peep的单曲《High School》 《High School》- lil peep 手机党少年们想听歌,请使劲儿戳(这里...

小小编辑
今天
943
13
Spring使用ThreadPoolTaskExecutor自定义线程池及实现异步调用

多线程一直是工作或面试过程中的高频知识点,今天给大家分享一下使用 ThreadPoolTaskExecutor 来自定义线程池和实现异步调用多线程。 一、ThreadPoolTaskExecutor 本文采用 Executors 的工厂...

CREATE_17
今天
12
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部