介绍: ed是Linux中面向行的文本编辑器,与sed区别修改过程中不需要创建临时文件,不需要重定向到另一文件中。
测试数据data文件
172.16.5.1 node1 node1.somedomain.com alias1.somedomain.com
172.16.5.2 node2 node2.somedomain.com alias2.somedomain.com
172.16.5.3 node3 node3.somedomain.com alias3.somedomain.com
172.16.5.4 node4 node4.somedomain.com alias4.somedomain.com
1. #全局/search/查找/search/replace/全局(全局替换)
ed -s ./data <
g/newdomain/s/newdomain/somedomain/g
w
q
EOF
2. #/search/查找/search/replace/全局(首条匹配行替换)
ed -s ./data <
/somedomain/s/somedomain/newdomain/g
w
q
EOF
3. #/search/查找/search/replace/(匹配行首个替换)
ed -s ./data <
/somedomain/s/somedomain/newdomain/
w
q
EOF
4. #/search/查找/search/replace/(所有匹配行行替换)
ed -s ./data <
g/172.16.5.2/s/somedomain/newdomain/g
w
q
EOF
#/search/查找/search/replace/(符合正则条件首行替换)
ed -s ./data <
g/^172.16.5/s/somedomain/newdomain/g
w
q
EOF
5. #/search/i/文本/.(在node1匹配行前插入行)
ed -s ./data <
/node1
i
172.16.5.4 node4 node4.newdomain.com alias4.newdomain.com
.
w
q
EOF
6. #/search/i/文本/.(在后追加行)
ed -s ./data <
/node2
a
172.16.5.4 node4 node4.newdomain.com alias4.newdomain.com
.
w
q
EOF
7. #/search/d(删除行)
ed -s ./data <
/node2
d
w
q
EOF
8.利用ed进行字符转义。
#!/bin/bash
usage="Usage: $0 "
if [ $# -eq 0 ]
then
echo $usage
fi
if [ ! -f $1 ]
then
echo "$1 does not exist"
echo $usage
exit 1
else
file=$1
fi
ed -s $file<
g/[\]/s/[\]/\\\\\\\/g
g/&/s/&/\\\&/g
g/!/s/!/\\\!/g
g/@/s/@/\\\@/g
g/#/s/#/\\\#/g
g/%/s/%/\\\%/g
g/,/s/,/\\\,/g
g/[$]/s/[$]/\\\\$/g
g/[&]/s/[&]/\\\\&/g
g/[*]/s/[*]/\\\\*/g
g/\\;/s/\\;/\\\;/g
g/\\:/s/\\:/\\\:/g
g/\\[/s/\\[/\\\[/g
g/\\]/s/\\]/\\\]/g
g/\\^/s/\\^/\\\^/g
g/\\;/s/\\;/\\\;/g
g/\\;/s/\\;/\\\;/g
g/[/]/s/[/]/\\\\//g
g/[(]/s/[(]/\\\(/g
g/[)]/s/[)]/\\\)/g
g/[+]/s/[+]/\\\+/g
g/[=]/s/[=]/\\\=/g
g/[{]/s/[{]/\\\{/g
g/[}]/s/[}]/\\\}/g
g/[]]/s/[]]/\\\}/g
g/[|]/s/[|]/\\\|/g
g/[']/s/[']/\\\'/g
g/["]/s/["]/\\\"/g
g/[<]/s/[<]/\\\
g/[>]/s/[>]/\\\>/g
g/[?]/s/[?]/\\\?/g
g/[~]/s/[~]/\\\~/g
g/[-]/s/[-]/\\\-/g
g/[_]/s/[_]/ \\\_/g
w
q
EOF
阅读(3508) | 评论(0) | 转发(0) |