sed:stream editor,流处理器。
每次处理一行的内容,处理时把行的内容存储到缓冲区(pattern space),接着用sed命令处理缓冲区的内容,处理之后输出到屏幕,不改变原文件的内容。
使用格式: sed 'cmd' file
关键是单引号括起来的部分,cmd运用的熟练能大大提高效率。
常例: 删/增
eg1: 删除含有字符‘a’的行
➜ shellQa cat data.txt
a 2
b 4
c 6
d 8
➜ shellQa sed '/a/d' data.txt
b 4
c 6
d 8
➜ shellQa cat data.txt 原文件内容未改变
a 2
b 4
c 6
d 8
eg2: 在每行的行首添加字符 '#'
➜ shellQa cat data.txt
a 2
b 4
c 6
d 8
➜ shellQa sed 's/^/#&/g' data.txt
#a 2
#b 4
#c 6
#d 8
eg3: 在每行的行尾添加字符 '#'
➜ shellQa cat data.txt
a 2
b 4
c 6
d 8
➜ shellQa sed 's/$/&#/g' data.txt
a 2#
b 4#
c 6#
d 8#