在編寫一個需要頻繁對配置文件進行編輯的模塊中的時候,多次使用了sed指令,現在總結一下
sed -i '/shell/s/^#//' /etc/inetd.conf
sed -i '/shell/s/^/#&/' /etc/inetd.conf
sed -i '$d' /etc/inetd.conf
以上指令分別為刪除行首#
注釋,在行首添加#
注釋,刪除最后一行
-i表示在原始文件上進行修改。
s/^#//表示將字符串開頭的#字符替換為空(即去除行首的#字符)
/shell/表示匹配含有shell字符串的行,也可以為正則表達式
1、刪除文檔的第一行
sed -i '1d' <file>
2、刪除文檔的最后一行
sed -i '$d' <file>