sed 命令使用及示例

sed 是一个用来筛选与转换文本内容的工具。通常用来批量替换,删除某行文件linux

sed 命令详解

每一个 sed 命令,基本能够由选项,匹配与对应的操做来完成git

# 打印文件第三行到第五行
# -n: 选项,表明打印
# 3-5: 匹配,表明第三行到第五行
# p: 操做,表明打印
$ sed -n '3,5p' file
 # 删除文件第二行
# -i: 选项,表明直接替换文件
# 2: 匹配,表明第二行
# d: 操做,表明删除
$ sed -i '2d' file
复制代码

选项

-n: 打印匹配内容行 -i: 直接替换文本内容 -f: 指定 sed 脚本文件,包含一系列 sed 命令github

匹配

  • /reg/: 匹配正则
  • 3: 数字表明第几行
  • $: 最后一行
  • 1,3: 第一行到第三行
  • 1,+3: 第一行,并再往下打印三行 (打印第一行到第四行)
  • 1, /reg/ 第一行,并到匹配字符串行

操做

  • a: append, 下一行插入内容
  • i: insert, 上一行插入内容
  • p: print,打印,一般用来打印文件某几行,一般与 -n 一块儿用
  • s: replace,替换,与 vim 一致

sed examples

查看手册

$ man sed
复制代码

打印特定行

p 指打印shell

# 1p 指打印第一行
$ ps -ef | sed -n 1p
UID        PID  PPID  C STIME TTY          TIME CMD
 # 2,5p 指打印第2-5行
$ ps -ef | sed -n 2,5p
root         1     0  0 Sep29 ?        00:03:42 /usr/lib/systemd/systemd --system --deserialize 15
root         2     0  0 Sep29 ?        00:00:00 [kthreadd]
root         3     2  0 Sep29 ?        00:00:51 [ksoftirqd/0]
root         5     2  0 Sep29 ?        00:00:00 [kworker/0:0H]
复制代码

打印最后一行

$ 指最后一行vim

注意须要使用单引号bash

$ ps -ef | sed -n '$p'
复制代码

删除特定行

d 指删除服务器

$ cat hello.txt
hello, one
hello, two
hello, three
 # 删除第三行内容
$ sed '3d' hello.txt
hello, one
hello, two
复制代码

过滤字符串

grep 相似,不过 grep 能够高亮关键词app

$ ps -ef | sed -n /ssh/p
root      1188     1  0 Sep29 ?        00:00:00 /usr/sbin/sshd -D
root      9291  1188  0 20:00 ?        00:00:00 sshd: root@pts/0
root      9687  1188  0 20:02 ?        00:00:00 sshd: root@pts/2
root     11502  9689  0 20:08 pts/2    00:00:00 sed -n /ssh/p
root     14766     1  0 Sep30 ?        00:00:00 ssh-agent -s
 $ ps -ef | grep ssh
root      1188     1  0 Sep29 ?        00:00:00 /usr/sbin/sshd -D
root      9291  1188  0 20:00 ?        00:00:00 sshd: root@pts/0
root      9687  1188  0 20:02 ?        00:00:00 sshd: root@pts/2
root     12200  9689  0 20:10 pts/2    00:00:00 grep --color=auto ssh
root     14766     1  0 Sep30 ?        00:00:00 ssh-agent -s
复制代码

删除匹配字符串的行

$ cat hello.txt
hello, one
hello, two
hello, three
 $ sed /one/d hello.txt
hello, two
hello, three
复制代码

替换内容

s 表明替换,与 vim 相似ssh

$ echo hello | sed s/hello/world/
world
复制代码

添加内容

ai 表明在新一行添加内容,与 vim 相似工具

# i 指定前一行
# a 指定后一行
# -e 指定脚本
$ echo hello | sed -e '/hello/i hello insert' -e '/hello/a hello append'
hello insert
hello
hello append
复制代码

替换文件内容

$ cat hello.txt
hello, world
hello, world
hello, world
 # 把 hello 替换成 world
$ sed -i s/hello/world/g hello.txt
 $ cat hello.txt
world, world
world, world
world, world
复制代码

注意事项

若是想在 mac 中使用 sed,请使用 gsed 替代,否则在正则或者某些格式上扩展不全。

使用 brew install gnu-sed 安装

$ echo "hello" | sed "s/\bhello\b/world/g"
hello
$ brew install gnu-sed
$ echo "hello" | gsed "s/\bhello\b/world/g"
world
复制代码

参考

相关文章
相关标签/搜索