linux shell 讀取配置文件

隨着linux接觸的越來越多,我們難免需要從一些配置文件中進行讀取配置參數,linux中shell屬於腳本型語言,讀取時沒有其它語言方便,特將用過的一種方式分享給大家

實戰代碼:

$ more a.txt
name=hello world
age=22
ip=192.168.1.1

$ sed '/^name=/!d; s/.*=//' a.txt
hello world
$ sed '/^age=/!d; s/.*=//' a.txt
22
$ sed '/^ip=/!d; s/.*=//' a.txt
192.168.1.1

腳本講解:
sed '/^name=/!d; s/.*=//' a.txt爲例,這裏面實際上執行了2次sed分別是

$ sed '/^name=/!d ' a.txt
name=hello world
bogon:temp didi$ sed 's/.*=//' a.txt
hello world
22
192.168.1.1

聰明伶俐的你一定看出來了,2次sed是以“;”進行分開執行的,
前面的’/^name=/!d '屬於正則匹配,得到滿足規則的行
後面的’s/.*=//'屬於正則替換,將“=”及“=”前面的內容替換掉(由於//之間沒有內容,相當於將“=”及“=”前面的內容刪除掉)

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