sed用法及举例

基本sed编程举例

-n 只输出处理后的行

-i 直接修改读取的文件内容,而不是输出到终端

-r 使用扩展正则表达式

打印指定行

[root@tyzZ SED]# cat file 
xdtn 6/56    12356   G   8   34  78
zdyn 6/56    12356   G   8   34  78
aklm 6/56    12356   G   8   34  78
xcvg 6/56    12356   G   8   34  78
uiol 6/56    12356   G   8   34  78
bghj 6/56    12356   G   8   34  78
asd  123     $q12    F   9  qwe dfgg
zxce 6/56    12356   G   8   34  78
[root@tyzZ SED]# sed -n 2p file 
zdyn 6/56    12356   G   8   34  78
[root@tyzZ SED]# sed -n "2,4"p file 
zdyn 6/56    12356   G   8   34  78
aklm 6/56    12356   G   8   34  78
xcvg 6/56    12356   G   8   34  78

打印匹配指定字母或单词的行

[root@tyzZ SED]# sed -n '/$/'p file    #匹配元字符
asd  123     $q12    F   9  qwe dfgg
[root@tyzZ SED]# sed -n '/aklm/'p file 
aklm 6/56    12356   G   8   34  78

显示所有行

[root@tyzZ SED]# sed -n '1,$p' file     #$意思是最后一行
xdtn 6/56    12356   G   8   34  78
zdyn 6/56    12356   G   8   34  78
aklm 6/56    12356   G   8   34  78
xcvg 6/56    12356   G   8   34  78
uiol 6/56    12356   G   8   34  78
bghj 6/56    12356   G   8   34  78
asd  123     $q12    F   9  qwe dfgg
zxce 6/56    12356   G   8   34  78

显示第一行

[root@tyzZ SED]# sed -n '1p' file 
xdtn 6/56    12356   G   8   34  78

显示最后一行

[root@tyzZ SED]# sed -n '$p' file 
zxce 6/56    12356   G   8   34  78

打印行号

使用“=”
[root@tyzZ SED]# sed -n '/uiol/=' file 
5
如果打印行号及匹配行的话 需要两条sed命令

删除文本

[root@tyzZ SED]# sed '1d' file        
zdyn 6/56    12356   G   8   34  78
aklm 6/56    12356   G   8   34  78
xcvg 6/56    12356   G   8   34  78
uiol 6/56    12356   G   8   34  78
bghj 6/56    12356   G   8   34  78
asd  123     $q12    F   9  qwe dfgg
zxce 6/56    12356   G   8   34  78

删除其他与显示其他 规则相同

替换文本

[root@tyzZ SED]# sed 's/xdtn/XDTN/' file 
#将文件中所有的xdtn替换为XDTN

删除文本

[root@tyzZ SED]# sed 's/$//' file
#将文本中所有的$删去

全局替换

[root@tyzZ SED]# sed 's/78/bb/g' file

将替换后的结果 写入到文件

[root@tyzZ SED]# sed 's/78/bb/w asd' file
XDTN 6/56    12356   G   8   34  bb
zdyn 6/56    12356   G   8   34  bb
aklm 6/56    12356   G   8   34  bb
xcvg 6/56    12356   G   8   $34  bb
uiol 6/56    $12356   G   8   34  bb
bghj 6/56    12356   G   8   34  bb
asd  123     $q12    F   9  qwe dfgg
$zxce 6/56    12356   G   8   34  bb

[root@tyzZ SED]# cat asd 
XDTN 6/56    12356   G   8   34  bb
zdyn 6/56    12356   G   8   34  bb
aklm 6/56    12356   G   8   34  bb
xcvg 6/56    12356   G   8   $34  bb
uiol 6/56    $12356   G   8   34  bb
bghj 6/56    12356   G   8   34  bb
$zxce 6/56    12356   G   8   34  bb

sed中,使用u表示大写,l表示小写

  1. 把每个单词的第一个小写字母变大写:
    sed 's/[a-z]/u&/g' filename
  2. 把所有小写变大写:
    sed 's/[a-z]/u&/g' filename
  3. 大写变小写:
    sed 's/[A-Z]/l&/g' filename
打印指定行数 (^开头)(结尾$)
[root@tyzz ~]# sed -n '1,10'p 1.txt 
root:x:0:0:root:/root:/bin/bash
123131231313123123131231313453453665789708
!@#!@#@$@$#%$%^%^&^*^(*)()**))^%^#$@$
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync

rr
匹配指定关键字
[root@tyzz ~]# sed -n '/root/'p 1.txt 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
删除匹配到的行。。。(d=delete)
[root@tyzz ~]# sed '/[0-9]/'d 1.txt 
!@#!@#@$@$#%$%^%^&^*^(*)()**))^%^#$@$

rr
rrr
rrrr
删除空行
[root@tyzz ~]# sed '/^$/'d 1.txt 

[root@tyzz ~]# sed '/^$/'d 3.txt 
i
adsads
替换
[root@tyzz ~]# sed 's/nologin/aaaaa/g' 1.txt 
root:x:0:0:root:/root:/bin/bash
123131231313123123131231313453453665789708
!@#!@#@$@$#%$%^%^&^*^(*)()**))^%^#$@$
bin:x:1:1:bin:/bin:/sbin/aaaaa

rr
rrr
rrrr
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/aaaaa
operator:x:11:0:operator:/root:/sbin/aaaaa

ftp:x:14:50:FTP User:/var/ftp:/sbin/aaaaa
nobody:x:99:99:Nobody:/:/sbin/aaaaa

dbus:x:81:81:System message bus:/:/sbin/aaaaa
polkitd:x:999:999:User for polkitd:/:/sbin/aaaaa

ntp:x:38:38::/etc/ntp:/sbin/aaaaa
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/aaaaa
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/aaaaa
libstoragemgmt:x:998:997:daemon account for libstoragemgmt:/var/run/lsm:/sbin/aaaaa
将数字去掉(将数字替换为空)
[root@tyzz ~]# sed 's/[0-9]//g' 1.txt 
root:x:::root:/root:/bin/bash

!@#!@#@$@$#%$%^%^&^*^(*)()**))^%^#$@$
bin:x:::bin:/bin:/sbin/nologin

rr
rrr
rrrr
shutdown:x:::shutdown:/sbin:/sbin/shutdown
去掉所有大小写字母
sed 's#[a-zA-z]##g' 1.txt
::0:0::/://
123131231313123123131231313453453665789708
!@#!@#@$@$#%$%%&*(*)()**))%#$@$
::1:1::/://




::6:0::/://
所有小写字母 替换为大写
sed 's/[a-z]/u&/g' 1.txt
大写字母 替换为小写
sed 's/[A-Z]/l&/g' 1.txt
把每个单词的第一个小写字母变大写
[root@tyzz ~]# sed 's/[a-z]/u&/g' 3.txt 
Asdada Asdadas Zxczxc Ffghf
Asdasd Adad Cbbcvbghf
Reert
Hfh Vbnvnjghj
I
Adsads
原文地址:https://www.cnblogs.com/aallenn/p/6700587.html