sed 是一个用来筛选与转换文本内容的工具。一般用来批量替换,删除某行文件
sed 命令详解
每个 sed 命令,基本可以由选项,匹配与对应的操作来完成
# 打印文件第三行到第五行# -n: 选项,代表打印# 3-5: 匹配,代表第三行到第五行# p: 操作,代表打印$ sed -n '3,5p' file# 删除文件第二行# -i: 选项,代表直接替换文件# 2: 匹配,代表第二行# d: 操作,代表删除$ sed -i '2d' file
关键选项
-n: 打印匹配内容行-i: 直接替换文本内容-
匹配
/reg/: 匹配正则3: 数字代表第几行$: 最后一行1,3: 第一行到第三行1,+3: 第一行,并再往下打印三行 (打印第一行到第四行)-
操作
a: append, 下一行插入内容i: insert, 上一行插入内容p: print,打印,通常用来打印文件某几行,通常与-n一起用s: replace,替换,与 vim 一致sed examples
查看手册
$ man sed
打印特定行
p指打印# 1p 指打印第一行$ ps -ef | sed -n 1pUID PID PPID C STIME TTY TIME CMD# 2,5p 指打印第2-5行$ ps -ef | sed -n 2,5proot 1 0 0 Sep29 ? 00:03:42 /usr/lib/systemd/systemd --system --deserialize 15root 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]
打印最后一行
$指最后一行注意需要使用单引号
$ ps -ef | sed -n '$p'
删除特定行
d 指删除
$ cat hello.txthello, onehello, twohello, three# 删除第三行内容$ sed '3d' hello.txthello, onehello, two
过滤字符串
与 grep 类似,不过 grep 可以高亮关键词
$ ps -ef | sed -n /ssh/proot 1188 1 0 Sep29 ? 00:00:00 /usr/sbin/sshd -Droot 9291 1188 0 20:00 ? 00:00:00 sshd: root@pts/0root 9687 1188 0 20:02 ? 00:00:00 sshd: root@pts/2root 11502 9689 0 20:08 pts/2 00:00:00 sed -n /ssh/proot 14766 1 0 Sep30 ? 00:00:00 ssh-agent -s$ ps -ef | grep sshroot 1188 1 0 Sep29 ? 00:00:00 /usr/sbin/sshd -Droot 9291 1188 0 20:00 ? 00:00:00 sshd: root@pts/0root 9687 1188 0 20:02 ? 00:00:00 sshd: root@pts/2root 12200 9689 0 20:10 pts/2 00:00:00 grep --color=auto sshroot 14766 1 0 Sep30 ? 00:00:00 ssh-agent -s
删除匹配字符串的行
$ cat hello.txthello, onehello, twohello, three$ sed /one/d hello.txthello, twohello, three
替换内容
s 代表替换,与 vim 类似
$ echo hello | sed s/hello/world/world
添加内容
a 与 i 代表在新一行添加内容,与 vim 类似
# i 指定前一行# a 指定后一行# -e 指定脚本$ echo hello | sed -e '/hello/i hello insert' -e '/hello/a hello append'hello inserthellohello append
替换文件内容
$ cat hello.txthello, worldhello, worldhello, world# 把 hello 替换成 world$ sed -i s/hello/world/g hello.txt$ cat hello.txtworld, worldworld, worldworld, 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
