Sed 的使用方法

来源:互联网 发布:巨人网络策划笔试题 编辑:程序博客网 时间:2024/04/30 11:04
1>.Sed 读取数据
sed


A).使用sed
从一行开始查找一直到出现有某个词语的行结束
The honesuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendanc.
-->
//从第二行 到出现The的行
sed -n '2,/The/'p test.txt

B). 匹对字符
查找一个含有 $的字符的行
sed -n '/\$/'p test.txt
The honesuckle band played all night long for only $90.
-->
sed -n '/The/'p test.txt
The honesuckle band played all night long for only $90.
The local nurse Miss P.Neave was in attendanc.
-->

C).整篇显示
sed -n '1,$p' test.txt

D).任何单词的行
sed -n '/.*ing/'p test.txt

E).显示行号
sed -n -e '/music/=' test.txt

如果既显示行号又显示该行的内容可以使用
sed -n -e '/music/p' -e '/music/=' test.txt

F). 第一行与最后一行的显示
# the first line
sed -n '1p' test.txt
# and the last line
sed -n '$p' test.txt

2>.sed 的 添加字符和文件


3>.sed的替换与删除

The honesuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendanc.
-->
A).首字符的删除
#删除每行开始的T字母
sed 's/^T*//g' test.txt
#把经过输出到一个新的文件test1.txt中
sed 's/^T*//gw test1.txt' test.txt
w --文件名 将结果输出定向到一个文件
p --缺省情况下将被替换行写入标准输出,
n --不打印输出结果.
-->