linux常用命令之文本替换
1 vi
vi test_file
:%s/h/h1/g
注释:全文替换,将h替换为h1
:1,4s/h/h1/g
注释:将第1行到第4行的h替换为h1
:%s/\n/,/g
注释:将换行符替换为,
2 sed
sed -e 's/h/h1/g' test_file
注释:全文替换,将h替换为h1
sed -e '1,4s/h/h1/g' test_file
注释:将第1行到第4行的h替换为h1
sed ':a;N;$!ba;s/\n/,/g' test_file
注释:将换行符替换为,
sed 'nd' test_file
注释:删除第n行
sed -e '1s/h/h1/g;4s/h/h1/g' test_file
注释:同时进行多个替换,;分隔
如果想直接修改文件,增加 -i 参数;
3 直接替换
$ echo ${var//a/b}