sed

  1. Sed
    1. 概念
    2. 定位
    3. 指令

Sed

http://man.linuxde.net/sed

概念

  • 模式缓冲区 – 相当于工作区间
  • 暂存缓冲区 – 相当于工作区间的缓存(二级空间),用于高级操作

定位

sed -e '/notifyFailWarnGroup/s/-825498206/-989332591/' app.json

指令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
sed '/^a test$/{
$!{ N # append the next line when not on the last line
s/^a test\nPlease do not$/not a test\nBe/
# now test for a successful substitution, otherwise
#+ unpaired "a test" lines would be mis-handled
t sub-yes # branch_on_substitute (goto label :sub-yes)
:sub-not # a label (not essential; here to self document)
# if no substituion, print only the first line
P # pattern_first_line_print
D # pattern_ltrunc(line+nl)_top/cycle
:sub-yes # a label (the goto target of the 't' branch)
# fall through to final auto-pattern_print (2 lines)
}
}' alpha.txt

指令

命令 功能
a 在当前行后添加一行或多行
c 用此符号后的新文本替换当前行中的文本
i 在当前行之前插入文本
d 删除行
h 把模式空间里的内容复制到暂存缓冲区
H 把模式空间里的内容追加到暂存缓冲区
g 把暂存缓冲区里的内容复制到模式空间,覆盖原有的内容
G 把暂存缓冲区的内容追加到模式空间里,追加在原有内容的后面
l 列出非打印字符
p 打印行
n 读入下一输入行,并从下一条命令而不是第一条命令开始对其的处理
N 读入下一输入行,并从第一条命令开始对其的处理 (当前处理是2行的内容)
q 结束或退出sed
r 从文件中读取输入行
! 对所选行以外的所有行应用命令
s 用一个字符串替换另一个
g 在行内进行全局替换
w 将所选的行写入文件
x 交换暂存缓冲区与模式空间的内容
y 将字符替换为另一字符(不能对正则表达式使用y命令)
  • 删除指定行的上一行下一行
    sed -i -e :a -e '$!N;s/.*\n\(.*linuxeye.com\)/\1/;ta' -e 'P;D' test.txt

  • 前一行插入
    sed -i 'N;/4/i\sadfsafsdfsd' aa

  • 每10行插入一行字符
    sed '1~10i\wait;' your_file


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 jaytp@qq.com

💰

×

Help us with donation