“p” command prints the buffer (remember to use -n option with “p”) “d” command is just opposite, its for deletion. ‘d’ will delete the pattern space buffer and immediately starts the next cycle. Syntax: # sed 'ADDRESS'd filename # sed /PATTERN/d file
a 追加内容 sed ‘/匹配词/a\要加入的内容’ example.file(将内容追加到匹配的目标行的下一行位置)i 插入内容 sed ‘/匹配词/i\要加入的内容’ example.file 将内容插入到匹配的行目标的上一行位置)示例:#我要把文件的包含“linux.com”这个关键词的行前或行后加入一行,内容为“allow linux.cn” 1 #行前加2 sed -i '/allow linux.com/i\allow linux.cn' the.conf.file3 #行前后4 s
删除匹配的下一行到最后一行 [root@test200 ~]# cat test a b c d e f [root@test200 ~]# sed '/c/{p;:a;N;$!ba;d}' test a b c 定义一个标签a,匹配c,然后N把下一行加到模式空间里,匹配最后一行时,才退出标签循环,然后命令d,把这个模式空间里的内容全部清除. if 匹配"c" :a 追加下一行 if 不匹配"$" goto a 最后退出循环,d命令删除. 删除匹配行和匹配行后的2行
#!/bin/bash while read line do for file in /home/local/test/* do if test -f $file then sed -n "/${line}/p" $file >> /home/local/out.txt fi done done < macid.txt 任务:要从home/local/test 文件夹下面的文件 file_1.log file_2.log .... 文件中把包含macid.txt文件的
比如以下五行,我要将带有英文字母a的一行全部批量删除1234551243243123aa244123123981232137aa 2013-04-11 19:32 提问者采纳 我这里是英文版,你自己估计着对应一下 在文件中按 Ctrl + H (查找/替换) Find what 里面填入: ^.*aa.*$Replace with 里面为空最下面 Regular expression 勾选点 Replace All 这样全部带有 aa 的行都被清空,变为空行,如果你想删除全部空行 Fin