sed notes

打印hello.txt的内容(相当于cat)

sed -n p hello.txt
# -n sed会在处理每行时将待处理的文本打印出来,-n参数关闭了这个功能
p:命令表示打印当前行

打印第二行到最后一行

sed -n '2,$'p hello.txt

打印匹配”100”的行

sed -n '/100/'p hello.txt

append命令: \a, 匹配100的行后面加入”new line”

sed '/100/'a\ "new line" hello.txt

insert命令: \i, 匹配100的行前面加入”new line”

sed '/100/'i\ "new line" hello.txt

change命令: \c, 匹配100的行替换为”new line”

sed '/100/'c\ "new line" hello.txt

delete命令: d, 匹配100的行删除

sed '/100/'d hello.txt

substitute命令: s, 将100替换为hello

sed 's/100/hello/g' hello.txt
# g :全部

匹配以10开头的行,并替换为yes,并输出

sed -n 's/^10/yes/p'  hello.txt

取出文件中行手的行号与冒号 设hello.txt的内容为 1:#!/bin/sh 2:cat hello.txt 3:exit

sed -n -e 's/^[0-9]\{1,\}://g' hello.txt

Published: July 22 2012