Linux sed 命令详解

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

sed 可按照脚本的指令来处理、编辑文本文件。

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

语法:

sed [-hnV][-e

参数说明:

-e

-f

-h 或--help 显示帮助。

-n 或--quiet或--silent 仅显示script处理后的结果。

-V 或--version 显示版本信息。

动作说明:

a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~

c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!

d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚;

i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);

p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~

s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是啦!

元字符集:

^ 指定行的开始

$ 指定行的结尾

. 匹配一个非换行符的字符

* 匹配零个或多个字符

[] 匹配指定字符内的任一字符

[^] 匹配不在指定字符内的任一字符

.. 保存已匹配的字符

&  s/super/YY&yy/   super变成YYsuperyy   & 保存搜索字符用来替换其他字符

\< 词首定位符 /\

\> 词尾定位符 /my\>/  匹配包含以my结尾的单词的行

x\{m\} 连续m个x /9\{5\}/ 匹配包含连续5个9的行

x\{m,\} 至少m个x /9\{5,\}/  匹配包含至少连续5个9的行

x\{m,n\} 至少m个,但不超过n个x /9\{5,7\}/  匹配包含连续5到7个9的行

实例:

在testfile文件第四行后添加一行,并将结果输出到标准输出,在命令行提示符输入如下命令:

sed -e 4a\hellowolrd testfile

查看testfile 中的内容:

[root@127-0-0-1 yoon]# cat testfile.txt

HELLO LINUX!

Linux is a free unix-type opterating system.

This is a linux testfile!

good good study!

Linux test

使用sed命令后,输出结果如下:

[root@127-0-0-1 yoon]# sed -e 4a\helloworld testfile.txt

HELLO LINUX!

Linux is a free unix-type opterating system.

This is a linux testfile!

good good study!

helloworld

Linux test

[root@127-0-0-1 yoon]# sed -e '4a\hello world' testfile.txt

HELLO LINUX!

Linux is a free unix-type opterating system.

This is a linux testfile!

good good study!

hello world

Linux test

以行为单位的新增和删除

删除

将testfile的内容列出并且打印行号,同时删除第4-5行删除

[root@127-0-0-1 yoon]# nl testfile.txt

1

HELLO LINUX!

2

Linux is a free unix-type opterating system.

3

This is a linux testfile!

4

good good study!

5

hello world

6

Linux test

[root@127-0-0-1 yoon]# nl testfile.txt | sed '4,5d'

1

HELLO LINUX!

2

Linux is a free unix-type opterating system.

3

This is a linux testfile!

6

Linux test

sed的动作为'4,5d',d是删除,因为4-5行删除了,所以显示的数据没有4-5行,另外,原本应该要下达 sed -e 才对,没有 -e 也可以。同事注意,sed 后面接的动作,务必以 '' 两个单引号扩住。

只删除第二行:

[root@127-0-0-1 yoon]# nl testfile.txt | sed '2d'

1

HELLO LINUX!

3

This is a linux testfile!

4

good good study!

5

hello world

6

Linux test

删除第三行到最后一行:

[root@127-0-0-1 yoon]# nl testfile.txt | sed '3,$d'    ($代表最后一行)

1

HELLO LINUX!

2

Linux is a free unix-type opterating system.

新增

在第二行后面加上drink milk 字样(亦即是加在第三行):

[root@127-0-0-1 yoon]# nl testfile.txt | sed '2a drink milk'

1

HELLO LINUX!

2

Linux is a free unix-type opterating system.

drink milk

3

This is a linux testfile!

4

good good study!

5

hello world

6

Linux test

如果加入到第二行前:

[root@127-0-0-1 yoon]# nl testfile.txt | sed  '2i drink milk'

1

HELLO LINUX!

drink milk

2

Linux is a free unix-type opterating system.

3

This is a linux testfile!

4

good good study!

5

hello world

6

Linux test

如果是增加两行以上,在第二行后面加入两行字,例如,drink milk,eat pig :

[root@127-0-0-1 yoon]# nl testfile.txt | sed '2a drink milk\  (每一行之间都必须要以反斜杠『 \ 』来进行新行的添加)

> eat pig'

1

HELLO LINUX!

2

Linux is a free unix-type opterating system.

drink milk

eat pig

3

This is a linux testfile!

4

good good study!

5

hello world

6

Linux test

[root@127-0-0-1 yoon]# nl testfile.txt | sed '2a drink milk\neat pig'  (\n换行符)

1

HELLO LINUX.

2

This is a linux testfile.

drink milk

eat pig

3

good good study.

4

hello world.

5

Linux test.

6

#This is good !

追加一行的话,不需要换行符\n,只有追加多行的情况下才需要换行符,最后一行也无需添加换行符,添加的话会多出一个空格

在第四行添加一行:

[root@127-0-0-1 yoon]# sed -e '4a new world' testfile.txt

HELLO LINUX.

This is a linux testfile.

good good study.

hello world.

new world

Linux test.

#This is good !

在第四行追加两行:

[root@127-0-0-1 yoon]# sed -e '4a new world\nold world' testfile.txt

HELLO LINUX.

This is a linux testfile.

good good study.

hello world.

new world

old world

Linux test.

#This is good !

在第四行追加三行(两行文字和一行空格):

[root@127-0-0-1 yoon]# sed -e '4a new world\nold world\n' testfile.txt

HELLO LINUX.

This is a linux testfile.

good good study.

hello world.

new world

old world

Linux test.

#This is good !

添加一个空行:

[root@127-0-0-1 yoon]# sed -e '4a \\' testfile.txt

HELLO LINUX.

This is a linux testfile.

good good study.

hello world.

Linux test.

#This is good !

添加两个空行:

[root@127-0-0-1 yoon]# sed -e '4a \\n' testfile.txt

HELLO LINUX.

This is a linux testfile.

good good study.

hello world.

Linux test.

#This is good !

以行为单位的替换和显示

将第3-4行替换为 No 2-5 number

[root@127-0-0-1 yoon]# nl testfile.txt | sed '2,5c No 2-5 number' (通过这个方法可以取代整行)

1

HELLO LINUX!

No 2-5 number

6

Linux test

仅列出第2-3行:

[root@127-0-0-1 yoon]# nl testfile.txt | sed -n '2,3p'

2

Linux is a free unix-type opterating system.

3

This is a linux testfile!

数据的搜寻并显示

搜索 linux 关键字的行:

[root@127-0-0-1 yoon]# nl testfile.txt | sed '/linux/p'    (如果linux找到,除了输出所有行,还会匹配输出行)

1

HELLO LINUX!

2

Linux is a free unix-type opterating system.

3

This is a linux testfile!

3

This is a linux testfile!

4

good good study!

5

hello world

6

Linux test

使用 -n 的时候只输出包含模板的行

[root@127-0-0-1 yoon]# nl testfile.txt | sed -n '/linux/p'

3

This is a linux testfile!

数据的搜寻并删除

删除testfile中包含linux的行,其他行输出:

[root@127-0-0-1 yoon]# nl testfile.txt | sed '/linux/d'

1

HELLO LINUX!

2

Linux is a free unix-type opterating system.

4

good good study!

5

hello world

6

Linux test

数据的搜寻并执行命令

搜索 linux 对应的行,执行花括号中的命令,每个命令用分号分隔,并把 testfile 替换成 newtestfile,再输出这行:

[root@127-0-0-1 yoon]# nl testfile.txt | sed -n '/linux/{s/testfile/newtestfile/;p;q}'   (最后的 q 是退出)

3

This is a linux newtestfile!

数据的搜寻并替换

除了整行的处理模式之外,sed 还可以用行为单位进行部分数据的搜寻并替换,基本上 sed 的搜寻与替代的 vi 相当的类似,有点像这样:

sed 's/要被取代的字符串/新的字符串/g'

查看 newtestfile 文件内容:

[root@127-0-0-1 yoon]# cat newtestfile.txt

eth0 Link encap:Ethernet HWaddr 00:90:CC:A6:34:84

inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0

inet6 addr: fe80::290:ccff:fea6:3484/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

本机的ip是192.168.1.100,将 IP 前面的部分予以删除:

[root@127-0-0-1 yoon]# cat newtestfile.txt | grep 'inet addr:' | sed 's/^.*addr://g'

192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0

接下来则是删除后续的部分,亦即: 192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0,将 IP 后面的部分予以删除:

[root@127-0-0-1 yoon]# cat newtestfile.txt | grep 'inet addr:' | sed 's/^.*addr://g' | sed 's/Bcast.*$//g'

192.168.1.100

多点编辑  sed -e 'cmd' -e 'cmd' filename

一条 sed 命令,将第三行到末尾的数据删除,并将opterating替换成 newopterating:

[root@127-0-0-1 yoon]# nl testfile.txt

1

HELLO LINUX!

2

Linux is a free unix-type opterating system.

3

This is a linux testfile!

4

good good study!

5

hello world

6

Linux test

多条sed:

[root@127-0-0-1 yoon]# nl testfile.txt | sed '3,$d' | sed 's/opterating/newopterating/'

1

HELLO LINUX!

2

Linux is a free unix-type newopterating system.

一条sed:

[root@127-0-0-1 yoon]# nl testfile.txt | sed -e '3,$d' -e  's/opterating/newopterating/'

1

HELLO LINUX!

2

Linux is a free unix-type newopterating system.

( -e 表示多点编辑,第一个 -e 编辑第三行到最后一行删除,第二个 -e 搜索 opterating替换成 newopterating)

直接修改文件内容

将 testfile 文件中的每一行结尾为 . 修改为 !:

[root@127-0-0-1 yoon]# cat testfile.txt

HELLO LINUX.

This is a linux testfile.

good good study.

hello world.

Linux test.

[root@127-0-0-1 yoon]# sed 's/\.$/\!/g' testfile.txt

HELLO LINUX!

This is a linux testfile!

good good study!

hello world!

Linux test!

利用 sed 直接在最后一行输入 #This is good !

[root@127-0-0-1 yoon]# sed -i '$a #This is good !' testfile.txt

[root@127-0-0-1 yoon]# cat testfile.txt

HELLO LINUX.

This is a linux testfile.

good good study.

hello world.

Linux test.

#This is good !

linux sed 命令,Linux sed 命令详解相关推荐

  1. linux中grep命令 菜鸟教程,linux grep正则表达式与grep用法详解

    需要大家牢记:正则表达式与通配符不一样,它们表示的含义并不相同 正则表达式只是字符串的一种描述,只有和支持正则表达式的工具相结合才能进行字符串处理.本文以grep为例来讲解正则表达式. grep命令 ...

  2. linux mount命令参数及用法详解

    linux mount命令参数及用法详解 非原创,主要来自 http://www.360doc.com/content/13/0608/14/12600778_291501907.shtml. htt ...

  3. linux useradd(adduser)命令参数及用法详解(linux创建新用户命令)

    linux useradd(adduser)命令参数及用法详解(linux创建新用户命令) useradd可用来建立用户帐号.帐号建好之后,再用passwd设定帐号的密码.而可用userdel删除帐号 ...

  4. linux中group命令详解,linux groupmod命令参数及用法详解

    需要更改群组的识别码或名称时,可用groupmod指令来完成这项工作.接下来是小编为大家收集的linux groupmod命令参数及用法详解,希望能帮到大家. linux groupmod命令参数及用 ...

  5. linux ipset 流量,linux中ipset命令的使用方法详解

    linux中ipset命令的使用方法详解 发布时间:2020-10-25 17:07:19 来源:脚本之家 阅读:97 作者:lijiaocn 栏目:服务器 ipset介绍 iptables是在lin ...

  6. linux的usermod命令参数,linux usermod命令参数及用法详解

    linuxusermod命令参数及用法详解,linux修改用户账号信息命令,usermod可用来修改用户帐号的各项设定.接下来是小编为大家收集的linux usermod命令参数及用法详解,欢迎大家阅 ...

  7. linux两台服务器传输,Linux两台服务器之间高速数据传输命令:scp应用详解

    Linux两台服务器之间高速数据传输命令:scp应用详解 Linux scp命令用于Linux之间复制文件和目录到另外一台,这个命令在多台服务器之间传输还是非常有用的,速度也是非常快的.比window ...

  8. Linux Bash命令关于程序调试详解

    转载:http://os.51cto.com/art/201006/207230.htm 参考:<Linux shell 脚本攻略>Page22-23 Linux bash程序在程序员的使 ...

  9. linux中id命令的功能,Linux id命令参数及用法详解

    Linux id命令参数及用法详解--Linux查看当前登陆用户uid,gid. 命          令:id 功能说明:查看显示目前登陆账户的uid和gid及所属分组及用户名 语 法:id [-g ...

  10. linux 内存 参数,linux free命令参数及用法详解(linux查看内存命令)

    linux free命令参数及用法详解(linux查看内存命令) 2019年05月31日 | 萬仟网科技 | 我要评论 free指令会显示内存的使用情况,包括实体内存,虚拟的交换文件内存,共享内存区段 ...

最新文章

  1. java中collection方法_Java 8中的Collector toCollection()方法
  2. 2020年ACM Fellows出炉!颜水成、周昆、陈怡然等12位华人当选
  3. Android作业(Activitiy)
  4. What type of NoSQL database is best suited to store hierarchical data?【转】
  5. MYSQL统计和识别重复值
  6. [vue] v-once的使用场景有哪些?
  7. 8.1.1使用BlockingQueue和ArrayBlockingQueue
  8. 苹果ll是什么版本_如何鉴别自己的iPhone手机,是什么版本呢?国行,美版,还是韩版?...
  9. VirtualBox安装debian无法启动,正确的解决办法
  10. 惠普局域网共享打印机设置_已解决: hp1106局域网共享打印机共享 - 惠普支持社区 - 817337...
  11. HtmlHelper用法大全(上)
  12. fastbin attack学习总结
  13. T530-I7重装win10
  14. Java使用465端口发送邮件(绕过25端口限制)
  15. 阿里代运营一定要掌握的几大技能!
  16. P3396 哈希冲突 (根号算法)
  17. NG Toolset开发笔记--5GNR Resource Grid(10)
  18. 哈工大 编译原理 复习笔记
  19. 对iis写权限的利用
  20. 距阵乘以一个未知距阵得单位矩阵 怎么算_想要定制家具?板材会选吗,知道价格怎么计算比较合理吗?...

热门文章

  1. python各数据类型特点
  2. 浅谈监控易运维系统在金融信创国产化中的使用
  3. 华为消费者业务 CEO 余承东 兼任华为云与计算总裁
  4. redmi note 10pro参数配置
  5. 网站安全防止网站被挂木马“安全狗”
  6. 软件测试质量改进,软件测试与持续质量改进
  7. 分辨率高,清晰度高?
  8. ABAQUS对称边界条件XSYMM含义详解
  9. loki 日志管理的安装部署使用
  10. ros2添加自定义msg