sed'[address] a the-line-to-append' input-file
#在文件的第二行后加多一行------------------sed'2 a ----------------'passwd#在文件的第二行到第五行(包括第五行)之间的行后加多一行------------------sed'2,5 a ------------------'passwd#在文件的最后一行后加多一行-----------------sed'$ a ----------------'passwd#在root所在行后加一行--------------sed'/root/ a -------------'passwd
插入操作:i insert
# sed '[address] i the-line-to-insert' input-file#在文件的第二行前加多一行------------------sed'2 i ----------------'passwd#在文件的第二行到第五行(包括第五行)之间的行前加多一行------------------sed'2,5 i ------------------'passwd#在文件的最后一行前加多一行-----------------sed'$ i ----------------'passwd#在root所在行前加一行--------------sed'/root/ i -------------'passwd
替换命令:c change
# sed '[address] i the-line-to-insert' input-file#将文件的第二行替换为------------------sed'2 c ----------------'passwd#将文件的第二行到第五行(包括第五行)之间的行替换为------------------sed'2,5 c ------------------'passwd#将文件的最后一行替换为-----------------sed'$ c ----------------'passwd#将包含root的行替换为--------------sed'/root/ c -------------'passwd
匹配内容标记
# 匹配内容标记# 用正整数标记同一行中匹配的内容次数,g标记为全局标记# 将每行第二个匹配到的dog替换为cat,其余匹配到的dog不做改动echo"dog dog dog dog dog"|sed's/dog/cat/2'# 将每行所有匹配到的dog都替换为catecho"dog dog dog dog dog"|sed's/dog/cat/g'
# 迁移符合条件的文本# H 复制到剪切板# g 将剪切板中的数据覆盖# G 将剪切板中的数据追加到指定行# w 保存为文件# r 读取指定文件# a 追加指定内容# H复制到剪切板--d删除--$G追加到文末catpasswd|head-5|sed'/daemon/H;/daemon/d;$G'catpasswd|head-5|sed'/daemon/{H,d};$G'# 将1-5行许移到17行后cat-npasswd|sed'1,5H; 1,5d; 17G'# 将包含the的行另存为新文件sed'/the/w text.txt'passwd# 在包含the每行后添加hostnamesed'/the/ s/$/hostname/g'passwd# 在第三行后插入一个空白行sed'3G'passwd# 在第三行后插入新行,内容为NEWcat-npasswd|sed'3G;3 c NEW'sed'3 a NEW'passwd