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}