sed替換文本中的內容

linux 的shell是個很強大的東西。

用sed命令可以很輕鬆的將文件中的東西替換成自己想要的內容。 

例如,有一個測試文件test,裏面的xxx作爲要替換的內容。

hello world!
this is a document that used to test sed!
here i have a simbol xxx, the simbol xxx is a location used to be replaced.


把上面文件中的xxx替換成昨天的日期

sed -e "s/xxx/`date -d "1 day ago" +%Y%m%d`/g" > testsedresult


結果就爲

hello world!
this is a document that used to test sed!
here i have a simbol 20130825, the simbol 20130825 is a location used to be replaced.


sed 後面跟的字符串 "s/ 源內容/ 目標內容 /g" 表示替換的內容,還可以用正則表達式來匹配源內容

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章