三剑客之Sed关于 p、a、d、w、i 、c 模式的干货

文件处理三剑客——Sed

Sed 行编辑器

  • stream editor
  • 用来操作纯 ASCII 码的文本
  • Sed 一次处理一行内容
  • 处理时,把当前处理的行存储在临时缓冲区中,称之为“模式空间”(pattern space)
  • 可以指定仅仅处理哪些行,Sed 符合模式条件的处理,不符合条件的不予处理
  • 处理完成之后把缓冲区的内容送往屏幕
  • 接着处理下一行,这样不断重复,直到文件末尾

Sed 命令格式

  • sed [OPTION] {script(sed自带语法脚本)} [input-file]
  • sed [OPTION] ‘command’ [input-file]
  • sed [OPTION] -f script file

常用选项[OPTION]参数

  • 显示帮助信息
--help
  • 显示版本号
--version
  • 关闭所有 GNU 扩展
--posix
  • 不输出模式空间内容到屏幕,即不自动打印,配合编辑命令只打印符合条件字符串
-n, --quiet, --silent   
  • 多点编辑,可添加多个脚本到程序的运行列表
-e 脚本, --expression=脚本
  • 从指定文件中读取编辑脚本
-f 脚本文件(/PATH/SCRIPT_FILE), --file=脚本文件
  • 原文件编辑(直接修改原处理文件),不添加-i参数,不会对原文件进行修改
-i
  • 在脚本中使用扩展正则表达式
-r
  • 将输入文件视为各个独立的文件而不是一个长的连续输入
-s, --separate
  • 从输入文件读取最少的数据,更频繁的刷新输出
-u, --unbuffered

如果没有 -e, –expression, -f 或 –file 选项,那么第一个非选项参数被视为sed脚本。其他非选项参数被视为输入文件,如果没有输入文件,那么程序将从标准输入读取数据。

Sed 对字符的处理

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

猜你喜欢

转载自blog.csdn.net/sunny_future/article/details/80174530
今日推荐