文本处理之sed命令的基本用法

文本处理之sed命令的基本用法

常用的文本处理方式有三种,分别是:grep、awk、sed。那么本文就详细介绍一下sed的基本用法。

1.替换

语法:

[address]s/pattern/replacement/flags

这里修饰替换的标志 flags 是:

n    //1到512之间的一个数字,表示对文本模式中指定模式第 n 次出现的情况进行替换
g    //对模式空间的所有出现的情况进行全局更改。而没有是通常只有第一次出现的情况被取代
p    //打印模式空间的内容
W  file
     //将模式空间的内容写到文件 file 中

替换命令应用于与 address 匹配的行。如果没有指定地址,那么就应用于Pattern 匹配的所有行。如果正则表达式作为地址来提供,并且没有指定模式,那么替换命令匹配由地址匹配的内容。当替换命令是应用于同一个地址上的多个命令之一时,这可能会非常有用。

例如:现在有这么一篇短文

[root@kiwi222 ~]# cat kiwi
I believe there is a person who brings sunshine into you life. That person may have enough to spread around. But if you really have to wait for someone to bring you the sun and give you a good feeling, then you may have to wait a long time.
[root@kiwi222 ~]#

用n将第二次出现的you改为Jerry

[root@kiwi222 ~]# sed 's/you/Jerry/2' kiwi
I believe there is a person who brings sunshine into you life. That person may have enough to spread around. But if /Jerry/ really have to wait for someone to bring you the sun and give you a good feeling, then you may have to wait a long time.
[root@kiwi222 ~]# 

## 注:文中加左斜杠的就是改变的地方

用g将所有出现的you改为Jenny

[root@kiwi222 ~]# sed 's/you/Jenny/g' kiwi
I believe there is a person who brings sunshine into /Jenny/ life. That person may have enough to spread around. But if /Jenny/ really have to wait for someone to bring /Jenny/ the sun and give /Jenny/ a good feeling, then /Jenny/ may have to wait a long time.
[root@kiwi222 ~]# 

修改为空后用p将其打印

[root@kiwi222 ~]# sed 's/you//p' kiwi
I believe there is a person who brings sunshine into  life. That person may have enough to spread around. But if you really have to wait for someone to bring you the sun and give you a good feeling, then you may have to wait a long time.
I believe there is a person who brings sunshine into  life. That person may have enough to spread around. But if you really have to wait for someone to bring you the sun and give you a good feeling, then you may have to wait a long time.
[root@kiwi222 ~]# 

## 由于默认就是打印,所以当它加上p的时候就又会再打印一遍

和地址不同的是,地址需要一个作为定界符的斜杠 (/) ,而正则表达式可以用任意字符来分隔,只有换行符除外。因此,如果模式包含斜杠,那么可以选择另一个字符作为定界符,例如感叹号。

s!/123/abc!/456/def!

注意,定界符出现了3 次而且在 replacement 之后是必需的。不管使用哪种定界符,如果它出现在正则表达式中,或者在替换文本中,那么就用反斜杠来转义它。

Replacement 是一个字符串,用来替换与正则表达式匹配的内容。在 replacement 部分,只用下列字符有特殊含义:

&    //用正则表达式匹配的内容进行替换。
\n   //匹配第n个字串 (n 是一个数字),这个字串以前在 pattern 中用"\(" 和 "\)指定。
\    //当在替换部分包含“与“符号 (&),反斜杠 ()和替换命令的定界符时可用转义它们。 另外,它用于转义换行符并创建多行replacement 字符串。

例如:如下面这篇文章

[root@kiwi222 ~]# cat kiwi
I believe there is a person who brings sunshine into you life. That person may have enough to spread around. But if you really have to wait for someone to bring you the sun and give you a good feeling, then you may have to wait a long time.
[root@kiwi222 ~]# 

将第一行替换成kiwi

[root@kiwi222 ~]# cat kiwi
I believe there is a person who brings sunshine into you life. That person may have enough to spread around. But if you really have to wait for someone to bring you the sun and give you a good feeling, then you may have to wait a long time.
kiwi111:kiwi222
111:222
111
[root@kiwi222 ~]#
[root@kiwi222 ~]# sed '1s/.*/kiwi/' kiwi
kiwi
kiwi111:kiwi222
111:222
111
[root@kiwi222 ~]# 

用&将匹配到的所有you进行替换成!you!

[root@kiwi222 ~]# sed 's/you/\!&\!/g' kiwi
I believe there is a person who brings sunshine into !you! life. That person may have enough to spread around. But if !you! really have to wait for someone to bring !you! the sun and give !you! a good feeling, then !you! may have to wait a long time.

\n

[root@kiwi222 ~]# cat kiwi222 
kiwi111:kiwi222
111:222

[root@kiwi222 ~]# sed 's/\(.*\):\(.*\)/2:\1/' kiwi222 
2:kiwi111
2:111
[root@kiwi222 ~]# 
2.删除

/^$/d 删除空行

[root@kiwi222 ~]# cat kiwi
I believe there is a person who brings sunshine into you life. That person may have enough to spread around. But if you really have to wait for someone to bring you the sun and give you a good feeling, then you may have to wait a long time.


kiwi111:kiwi222
111:222



111
[root@kiwi222 ~]#
[root@kiwi222 ~]# sed /^$/d kiwi
I believe there is a person who brings sunshine into you life. That person may have enough to spread around. But if you really have to wait for someone to bring you the sun and give you a good feeling, then you may have to wait a long time.
kiwi111:kiwi222
111:222
111
[root@kiwi222 ~]# 

删除空行并删除kiwi111

[root@kiwi222 ~]# sed '/^$/d;/kiwi111/d' kiwi
I believe there is a person who brings sunshine into you life. That person may have enough to spread around. But if you really have to wait for someone to bring you the sun and give you a good feeling, then you may have to wait a long time.
111:222
111

删除第二行

[root@kiwi222 ~]# sed '2d' kiwi
I believe there is a person who brings sunshine into you life. That person may have enough to spread around. But if you really have to wait for someone to bring you the sun and give you a good feeling, then you may have to wait a long time.
111:222
111
[root@kiwi222 ~]# 

删除最后一行

[root@kiwi222 ~]# sed '$d' kiwi
I believe there is a person who brings sunshine into you life. That person may have enough to spread around. But if you really have to wait for someone to bring you the sun and give you a good feeling, then you may have to wait a long time.
kiwi111:kiwi222
111:222
[root@kiwi222 ~]# 
3.追加、插入和更改
[root@kiwi222 ~]# cat kiwi
I believe there is a person who brings sunshine into you life. That person may have enough to spread around. But if you really have to wait for someone to bring you the sun and give you a good feeling, then you may have to wait a long time.
kiwi111:kiwi222
111:222
111

将最后的111更改为555

[root@kiwi222 ~]# sed '/^111$/c555' kiwi
I believe there is a person who brings sunshine into you life. That person may have enough to spread around. But if you really have to wait for someone to bring you the sun and give you a good feeling, then you may have to wait a long time.
kiwi111:kiwi222
111:222
555

在111:222后面追加1234

[root@kiwi222 ~]# sed '/^111\:/a1234' kiwi
I believe there is a person who brings sunshine into you life. That person may have enough to spread around. But if you really have to wait for someone to bring you the sun and give you a good feeling, then you may have to wait a long time.
kiwi111:kiwi222
111:222
1234
111
[root@kiwi222 ~]# 

在kiwi111:kiwi222前面插入999

[root@kiwi222 ~]# sed '/^kiwi/i999' kiwi
I believe there is a person who brings sunshine into you life. That person may have enough to spread around. But if you really have to wait for someone to bring you the sun and give you a good feeling, then you may have to wait a long time.
999
kiwi111:kiwi222
111:222
111
[root@kiwi222 ~]# 
4.转换

将文本内容的大小写互相转换

[root@kiwi222 ~]# cat kiwi
I believe there is a person who brings sunshine into you life. That person may have enough to spread around. But if you really have to wait for someone to bring you the sun and give you a good feeling, then you may have to wait a long time.
kiwi111:kiwi222
111:222
111
[root@kiwi222 ~]# 

如将上面文章内容的abcdefghijk转换为ABCDEFGHIJK

[root@kiwi222 ~]# sed 'y/abcdefghijk/ABCDEFGHIJK/' kiwi
I BElIEvE tHErE Is A pErson wHo BrInGs sunsHInE Into you lIFE. THAt pErson mAy HAvE EnouGH to sprEAD ArounD. But IF you rEAlly HAvE to wAIt For somEonE to BrInG you tHE sun AnD GIvE you A GooD FEElInG, tHEn you mAy HAvE to wAIt A lonG tImE.
KIwI111:KIwI222
111:222
111
[root@kiwi222 ~]# 
5.打印

打印命令§输出模式空间的内容

[root@kiwi222 ~]# cat kiwi
I believe there is a person who brings sunshine into you life. That person may have enough to spread around. But if you really have to wait for someone to bring you the sun and give you a good feeling, then you may have to wait a long time.
kiwi111:kiwi222
111:222
111
[root@kiwi222 ~]# 

将文章内容的kiwi111进行打印

[root@kiwi222 ~]# sed  '/^kiwi111/p' kiwi
I believe there is a person who brings sunshine into you life. That person may have enough to spread around. But if you really have to wait for someone to bring you the sun and give you a good feeling, then you may have to wait a long time.
kiwi111:kiwi222
kiwi111:kiwi222
111:222
111
[root@kiwi222 ~]# 
6.下一步

下一步 (next) 命令 (n)输出模式空间的内容,然后读取输入的下 一行,而
不用返回到脚本的顶端。

[root@kiwi222 ~]# cat kiwi
I believe there is a person who brings sunshine into you life. That person may have enough to spread around. But if you really have to wait for someone to bring you the sun and give you a good feeling, then you may have to wait a long time.
kiwi111:kiwi222
111:222
111
[root@kiwi222 ~]# 

将文章内容的kiwi111:kiwi222的下一行的111:222改为222:222

[root@kiwi222 ~]# sed  '/^k/{n;s/111/222/}' kiwi
I believe there is a person who brings sunshine into you life. That person may have enough to spread around. But if you really have to wait for someone to bring you the sun and give you a good feeling, then you may have to wait a long time.
kiwi111:kiwi222
222:222
111
[root@kiwi222 ~]# 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值