温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何理解Linux sed命令

发布时间:2021-11-06 10:01:07 来源:亿速云 阅读:123 作者:柒染 栏目:建站服务器

本篇文章给大家分享的是有关如何理解Linux sed命令,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

Linux sed 命令详解

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

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

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 显示版本信息。

动作说明:

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开头的单词的行

\> 词尾定位符 /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

     1HELLO LINUX!

     2Linux is a free unix-type opterating system.

     3This is a linux testfile!

     4good good study!

     5hello world

     6Linux test

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

     1HELLO LINUX!

     2Linux is a free unix-type opterating system.

     3This is a linux testfile!

     6Linux test

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

只删除第二行:

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

     1HELLO LINUX!

     3This is a linux testfile!

     4good good study!

     5hello world

     6Linux test

删除第三行到最后一行:

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

     1HELLO LINUX!

     2Linux is a free unix-type opterating system.

新增

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

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

     1HELLO LINUX!

     2Linux is a free unix-type opterating system.

drink milk

     3This is a linux testfile!

     4good good study!

     5hello world

     6Linux test

如果加入到第二行前:

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

     1HELLO LINUX!

drink milk

     2Linux is a free unix-type opterating system.

     3This is a linux testfile!

     4good good study!

     5hello world

     6Linux test

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

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

> eat pig'

     1HELLO LINUX!

     2Linux is a free unix-type opterating system.

drink milk

eat pig

     3This is a linux testfile!

     4good good study!

     5hello world

     6Linux test

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

     1HELLO LINUX.

     2This is a linux testfile.

drink milk

eat pig

     3good good study.

     4hello world.

     5Linux 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' (通过这个方法可以取代整行)

     1HELLO LINUX!

No 2-5 number

     6Linux test

仅列出第2-3行:

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

     2Linux is a free unix-type opterating system.

     3This is a linux testfile!

 数据的搜寻并显示

 搜索 linux 关键字的行:

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

     1HELLO LINUX!

     2Linux is a free unix-type opterating system.

     3This is a linux testfile!

     3This is a linux testfile!

     4good good study!

     5hello world

     6Linux test

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

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

     3This is a linux testfile!

数据的搜寻并删除

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

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

     1HELLO LINUX!

     2Linux is a free unix-type opterating system.

     4good good study!

     5hello world

     6Linux test

 数据的搜寻并执行命令

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

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

     3This 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

     1HELLO LINUX!

     2Linux is a free unix-type opterating system.

     3This is a linux testfile!

     4good good study!

     5hello world

     6Linux test

多条sed:

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

     1HELLO LINUX!

     2Linux 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/'

     1HELLO LINUX!

     2Linux 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.

以上就是如何理解Linux sed命令,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI