歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> Linux命令之——替換文件裡的字符串

Linux命令之——替換文件裡的字符串

日期:2017/3/1 14:43:06   编辑:關於Linux
Linux命令之——替換文件裡的字符串 在Linux中,如果需要替換一個文本文件裡的某個字符串,需要用到sed這個命令。下面給出例子: 要把 test.txt文件裡的所有 desc 字符串替換為 helloworld,可用這樣的命令 sed "s#desc#helloworld#g" test.txt >test.txt.temp mv test.txt.temp test.txt 於是可以做出一個替換字符串的函數。 [plain] function replacePara() { old=$1 new=$2 file=$3 sed "s#$old#$new#g" $file>$file.temp mv $file.temp $file } 然後調用方法 replacePara desc helloworld test.txt 即可將test.txt裡的desc全部替換為helloworld
Copyright © Linux教程網 All Rights Reserved