sed常用命令

释义

sed是stream editor的缩写,中文称之为“流编辑器”。

sed command file

  • command部分,针对每行要进行的处理
  • file,要处理的文件

Sed 对字符的处理

p :显示,将某个选择的数据打印显示。通常 p 会与参数 sed -n 一起执行
a :添加, a 的后面可以接字符串,该字符串会在当前指定行的下一行出现
d :删除,显示模式空间删除指定行后的内容,不会对原文件数据删除
c :更改, c 的后面可以接字符串,该字符串可以取代 n1,n2 之间的行
i :插入, i 的后面可以接字符串,该字符串会在当前指定行的上一行出现
s :替换,通常 s 的动作,如 sed ‘s/old/new/g’ file
w :写入,指定行内容重定向写入到指定文件

可以理解为增删查改
增 a add
删除 d delete
查 p
改 c change
查找替换 s
w 重定向到指定文件

实例

1、查看 /etc/fstab 内容
[root@test ~]# cat /etc/fstab  

#
# /etc/fstab
# Created by anaconda on Sun Mar 11 21:00:48 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=92d21210-58d8-4924-bd39-cce40740fc3f /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
2、关于 p 模式(显示)
打印文件 /etc/fstab 中含有字符串 UUID 的行
[root@test ~]# sed -n '/UUID/p' /etc/fstab
UUID=92d21210-58d8-4924-bd39-cce40740fc3f /boot                   xfs     defaults        0 0
[root@test ~]# 

打印文件 /etc/fstab 中以 UUID 结尾的行
[root@test ~]# sed -n '/UUID$/p' /etc/fstab
[root@test ~]# 

打印文件 /etc/fstab 中以 UUID 开头的行
[root@test ~]# sed -n '/^UUID/p' /etc/fstab
UUID=92d21210-58d8-4924-bd39-cce40740fc3f /boot                   xfs     defaults        0 0

打印文件 /etc/fstab 中含有 : 的行
[root@test ~]# sed -n '/\:/p' /etc/fstab 
# Created by anaconda on Sun Mar 11 21:00:48 2018
打印文件 /etc/fstab 中的空行
[root@test ~]# sed -n '/^$/p' /etc/fstab 

[root@test ~]#

打印文件 /etc/fstab 中 2到6 行
[root@test ~]# sed -n '2,6p' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sun Mar 11 21:00:48 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'

打印文件 /etc/fstab 中 除了 2到6 行之外的其他行
[root@test ~]# sed -n '2,6!p' /etc/fstab

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=92d21210-58d8-4924-bd39-cce40740fc3f /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
3、关于 a 模式(添加)
找出文件 /etc/fstab 中以 UUID 开头的行,并在其下一行添加单行字符串(追加到指定行后)
[root@test ~]# sed '/^UUID/a\hello world'  /etc/fstab   #指定行的下一行,添加 hello world

#
# /etc/fstab
# Created by anaconda on Sun Mar 11 21:00:48 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=92d21210-58d8-4924-bd39-cce40740fc3f /boot                   xfs     defaults        0 0
hello world
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0

找出文件 /etc/fstab 中以 UUID 开头的行,并在其下一行添加多行字符串
[root@test ~]# sed '/^UUID/a\hello world\nhello sed'  /etc/fstab    #指定行的下一行,添加两行内容: hello world 与 hello sed 其中 \n 表示换行的意思

#
# /etc/fstab
# Created by anaconda on Sun Mar 11 21:00:48 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=92d21210-58d8-4924-bd39-cce40740fc3f /boot                   xfs     defaults        0 0
hello world
hello sed
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0

在文件 /etc/fstab 中指定第五行的下一行添加字符串
[root@test ~]# sed -e '5a\hello world' /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sun Mar 11 21:00:48 2018
#
hello world
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=92d21210-58d8-4924-bd39-cce40740fc3f /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0

在文件 /etc/fstab 中指定第一行、第五行的下一行分别添加字符串
[root@test ~]# sed -e '1a\hello sed'  -e '5a\hello world' /etc/fstab 

hello sed
#
# /etc/fstab
# Created by anaconda on Sun Mar 11 21:00:48 2018
#
hello world
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=92d21210-58d8-4924-bd39-cce40740fc3f /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
4、关于 d 模式(删除)
显示文件 /etc/fstab 在 模式空间删除 含有 UUID 字符串的行
[root@test ~]# sed  '/UUID/d' /etc/fstab

#
# /etc/fstab
# Created by anaconda on Sun Mar 11 21:00:48 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root   /                       xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0

显示文件 /etc/fstab 在 模式空间删除以 UUID 开头的行后的内容
[root@test ~]# sed  '/^UUID/d' /etc/fstab

#
# /etc/fstab
# Created by anaconda on Sun Mar 11 21:00:48 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root   /                       xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0

显示文件 /etc/fstab 在 模式空间删除除了以 UUID 开头之外的行后的内容
[root@test ~]# sed  '/^UUID/!d' /etc/fstab
UUID=92d21210-58d8-4924-bd39-cce40740fc3f /boot                   xfs     defaults        0 0
[root@test ~]# 

显示文件 /etc/fstab 在 模式空间删除以 # 开头的行后的内容
[root@test ~]# sed  '/^#/d' /etc/fstab

/dev/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=92d21210-58d8-4924-bd39-cce40740fc3f /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0

显示文件 /etc/fstab 在 模式空间删除 空行 后的内容
[root@test ~]# sed  '/^$/d' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sun Mar 11 21:00:48 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=92d21210-58d8-4924-bd39-cce40740fc3f /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0

显示文件 /etc/fstab 在 模式空间 删除 1到4行后的内容
[root@test ~]# sed  '1,4d' /etc/fstab
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=92d21210-58d8-4924-bd39-cce40740fc3f /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
5、关于 w 模式(写入)
将文件 /etc/fstab 中以 UUID 开头的行指定输出(重定向)到 /tmp/fstab.txt 且不显示到屏幕
[root@test ~]# sed -n '/^UUID/w /tmp/fstab.txt' /etc/fstab
[root@test ~]# cat /tmp/fstab.txt 
UUID=92d21210-58d8-4924-bd39-cce40740fc3f /boot
6、关于 c 模式(更改)
将 /etc/fstab 中含有字符串 UUID 的行替换为 hello world
[root@test ~]# sed '/UUID/c\hello world' /etc/fstab

#
# /etc/fstab
# Created by anaconda on Sun Mar 11 21:00:48 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root   /                       xfs     defaults        0 0
hello world
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0

将 /etc/fstab 中含有字符串以 UUID 开头的行替换为两行内容:hello world 与 hello sed
[root@test ~]# sed '/^UUID/c\hello world\nhello sed' /etc/fstab

#
# /etc/fstab
# Created by anaconda on Sun Mar 11 21:00:48 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root   /                       xfs     defaults        0 0
hello world
hello sed
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
7、关于 i 模式(插入)
找出文件 /etc/fstab 中以 UUID 开头的行,并在其上一行添加单行字符串(插入指定行)
[root@test ~]# sed '/^UUID/i\hello world' /etc/fstab

#
# /etc/fstab
# Created by anaconda on Sun Mar 11 21:00:48 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root   /                       xfs     defaults        0 0
hello world
UUID=92d21210-58d8-4924-bd39-cce40740fc3f /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
8、关于 s 用法(替换)
每行替换第一个指定字符串
sed ‘s/要被替代的字符串/新的字符串/’ (斜杠为定界符)

将每行第一个 / 转为 $ (反斜杠为转义字符即 \ / 相当于 / )
[root@test ~]# sed 's/\//$/' /etc/fstab  

#
# $etc/fstab
# Created by anaconda on Sun Mar 11 21:00:48 2018
#
# Accessible filesystems, by reference, are maintained under '$dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and$or blkid(8) for more info
#
$dev/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=92d21210-58d8-4924-bd39-cce40740fc3f $boot                   xfs     defaults        0 0
$dev/mapper/rhel-swap   swap                    swap    defaults        0 0

全文替换所有指定字符串
sed ‘s/要被取代的字符串/新的字符串/g’ (斜杠为定界符,g 对模式空间指定情况的全局更改)

将所有的 / 转为 $ (反斜杠为转义字符即 \ / 相当于 / )
[root@test ~]# sed 's/\//$/g' /etc/fstab

#
# $etc$fstab
# Created by anaconda on Sun Mar 11 21:00:48 2018
#
# Accessible filesystems, by reference, are maintained under '$dev$disk'
# See man pages fstab(5), findfs(8), mount(8) and$or blkid(8) for more info
#
$dev$mapper$rhel-root   $                       xfs     defaults        0 0
UUID=92d21210-58d8-4924-bd39-cce40740fc3f $boot                   xfs     defaults        0 0
$dev$mapper$rhel-swap   swap                    swap    defaults        0 0
9、打印文件中的行号
打印指定行号(模式空间中的指定行),如以 UUID 开头的行(行号显示在其上一行)
[root@test ~]# sed '/^UUID/='  /etc/fstab

#
# /etc/fstab
# Created by anaconda on Sun Mar 11 21:00:48 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root   /                       xfs     defaults        0 0
10
UUID=92d21210-58d8-4924-bd39-cce40740fc3f /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0

打印文件的总行数
[root@test ~]# sed -n '$=' /etc/fstab 
11
[root@test ~]# 
打印日志文件中出现错误信息的行号及其内容
[root@test ~]# sed -n '/error/{=;p}'  /var/log/messages
865
Mar 11 20:15:15 localhost kdumpctl: cat: write error: Broken pipe
1858
Apr 11 20:51:51 localhost kdumpctl: cat: write error: Broken pipe

正则常用

显示

sed -n '10p' file		# 显示第10行
sed -n '10,20p' file	# 显示第10到20之间的行
sed -n '/pattern/p' file# 显示含有pattern的行
sed -n '/pattern1/,/pattern2/p' file # 显示patter1与pattern2之间的行

删除

sed '10d' file			# 删除第10行
sed '10,20d' file		# 删除第10到20之间的行
sed '/pattern/d'		# 删除匹配pattern的行
sed '/^\s*$/d' file		# 删除空白行

替换

sed 's/^\s*//' file		# 删除行前的空白:空格,制表符
sed 's/\s*$//' file		# 删除行尾的空白:空格,制表符
sed 's/^\s*//;s/\s*$//' file # 删除行首和行尾的空白:空格,制表符
sed 's/AA/BB/' file		# 将文件中的AA替换成BB,只替换一行中第一次出现的AA,替换后的结果输出到屏幕
sed 's/AA/BB/g' file	# 将文件中的所有AA都替换成BB,替换后的结果输出到屏幕
sed -i 's/AA/BB/g' file # 将文件中的所有AA都替换成BB,直接更改文件的内容
sed '/CC/s/AA/BB/g' file# 只替换那些含有CC的行
sed 's/pattern/&XXXX/' file	# 在pattern之后加上XXXX。&表示之前被匹配的内容
sed 's/pattern.*/&XXXX' file# 在匹配pattern的行尾加上XXXX。pattern.*表示包含pattern的整行内容
sed 's/^/添加的头部&/g'      #在所有行首添加
sed 's/$/&添加的尾部/g'      #在所有行末添加
sed '2s/原字符串/替换字符串/g'  #替换第2行
sed '$s/原字符串/替换字符串/g'   #替换最后一行
sed '2,5s/原字符串/替换字符串/g' #替换2到5行
sed '2,$s/原字符串/替换字符串/g' #替换2到最后一行
sed s/[[:space:]]//g           # 将每行中的空格进行去重
sed ':a;N;$!ba;s/\n//g'        # 将最后的换行符替换成空格
sed ':a;N;$!ba;s/88/--/' 通过循环把文本读入pattern space并将文本中的第一行中的88替换成--
sed -n '1~4s/^@/>/p;2~4p' file.fq > file.fa	# Fastq文件转Fasta文件
sed -n '2~4p' file.fq		# 提取Fastq文件的序列

sed 'y/ABC/XYZ/' file	# 将ABC逐字替换成XYZ

修改

sed '1i\hello' file		# 在第1行前面插入一行,内容为hello,通常用来为文件增加标题
sed '1a\hello' file		# 在第1行后面插入一行,内容为hello
sed '1r file2' file1	# 在第1行后面读入file2的内容

文件写入

sed ‘/pattern/w file2’ file1 # 将匹配的行写入file2中

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

独步秋风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值