sed 笔记

Table of Contents


说明

本文使用的版本为 sed (GNU sed) 4.2.2, 其他版本不保证所有的功能和示例测试通过.
查看版本号命令: sed --version |sed -n 1p

macOS 兼容性

# brew install gnu-sed
[[ $(uname) == "Darwin" ]] && PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"

基本使用方式

  • sed [-n] [-e] ’command(s)’ files

        sed '1d' books.txt # 删除第1行
        sed '1d;2d;5d;' books.txt # 删除第1,2,5行
        sed -e '1d' -e '2d' -e '5d' books.txt # 删除1,2,5行
    
  • sed [-n] -f scriptfile files

        echo -e "1d\n2d\n5d" > commands.txt
        sed -f commands.txt books.txt
    

部分参数说明

-n, --quiet, --silent
	suppress automatic printing of pattern space
	抑制模式空间的自动打印
-e script, --expression=script
	add the script to the commands to be executed
	添加执行命令需要的脚本
-f script-file, --file=script-file
	add the contents of script-file to the commands to be executed
	添加执行命令需要的脚本文件
-i[SUFFIX], --in-place[=SUFFIX]
        edit files in place (makes backup if SUFFIX supplied)
        修改后将覆盖原文件 (如果指定了'SUFFIX', 将会备份修改前的文件, 新的文件名会在原来的基础上以'SUFFIX'为后缀)

地址

addr1

number 匹配指定数字的行
first~step 从第first行开始的, 匹配每个step倍数行. 例如:50~5, 将会匹配(50,55,60,65…)行
$ 匹配最后一行
/regexp/ 匹配所有’/regexp/’的行

addr2

n1,n2 匹配[n1, n2]行
n1,+n2 匹配[n1, n1+n2]行
n,/regexp/ 从第n行开始, 匹配至第一个 /regexp/ 的行
/regexp/,n 从第一个 /regexp/ 的行开始, 匹配到第n行. 并在余下的行中匹配所有 /regexp/
/regexp/,+n 循环执行: 从下一个 /regexp/ 的行开始, 匹配接下来的连续n行
/regexp/,~n 循环执行: 从下一个 /regexp/ 的行开始, 匹配至第一个行号是n的倍数
/regexp1/,/regexp2/ 循环执行: 从下一个 /regexp1/ 的行开始, 匹配至第一个 /regexp2/ 的行

命令

Substitute

文本替换

[address1[,address2]]s/pattern/replacement/[flags]
  1. [flags] 用法:

    g 匹配全部
    2 匹配第2次
    3g 匹配除了前2次
    i 忽略大小写
    p 打印改变的行
    w file 改变的行保存到文件
  2. pattern 中, 被 \( \) 包围的部分.
    replacement 中, 可以以 \N 的方式提取, 其中 N 表示第几个匹配

       $ echo "Three One Two" | sed 's/\(\w\+\) \(\w\+\) \(\w\+\)/\2 \3 \1/'
       One Two Three
    

Insert

插入行

[address]i\
Insert text

Append

追加行

[address]a\
Append text

Delete

删除行

[address1[,address2]]d

Change

改变行

[address1[,address2]]c\
Replace text

Translate

替换字符

[address1[,address2]]y/list-1/list-2/

l

显示特殊字符, [len] 表示每行显示几个字符

[address1[,address2]]l
[address1[,address2]]l [len]

Write

写入文件
sed -n 'w b.txt' a.txt 等价于 cp a.txt b.txt

[address1[,address2]]w file

Read

读取文件

[address]r file

Execute

执行命令, 如果 [command] 省略, 将会执行 处理的文件 里面的命令

[address1[,address2]]e [command]

Quit

退出sed, [value] 表示返回码

[address]q
[address]q [value]

=

行号
sed -n '$=' a.txt 等价于 cat a.txt |wc -l

[address1[,address2]]=

&

存储之前的成功匹配

参考

Author: Saul Lawliet

Created: 2022-03-17 Thu 11:08