sed常用实例

sed 常用实例

1. 替换文件中的数据

把文件中还有“today”字符串的数据行中的”Tom”字符串替换成“John”:

sed -e '/today/s/Tom/John/g' input.dat

把文件中第三行 的数据替换成句子“Young is better”:

sed -e '5c\
Young is better
' input.dat

把文件中第3至10行的数据,替换成如下两行资料”Young it better” “Haha”:

sed -e '3,10c\
Young is better\
Haha
' input.dat

2. 搬动文件中的数据

将文件中的前40行数据,搬到文件中第300后输出:

sed -f mov.scr

mov.scr 文件的内容:

1,40{
H
d
}
300G

3. 删除文件中的数据

将文件中所有空白行全部删除:

sed -e '/^$/d' input.dat

将文件内连续的空白行,删除他们为一行:

sed -e '/^$/{
N
/^$/D
}' input.dat

4. 搜素文件中的数据

将文件中含有”Tom”字符串的数据行输出:

sed -n -e '/Tom/p' input.dat

将文件中含有”omega”字符串的数据输出:

sed -f gp.scr

gp.scr内容为:

/omega/b
N
h
s/.*\n//
/omega/b
g
D
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章