2012-08-30 31 views

回答

6

從技術上講,這將工作:

sed '/;$/!s/$/;/' input 

但你可能關心尾隨空白,所以:

sed '/; *$/!s/$/;/' input 

如果您的sed支持\s

sed '/;\s*$/!s/$/;/' input 

或者你可能會使用:

sed '/;[[:space:]]*$/!s/$/;/' input 
2

使用的sed:

sed -i '/[^;] *$/s/$/;/' input_file 

這意味着:

-i   overwrite the original file with new contents 
/[^;] *$/ find lines that do not contain a `;` at the end (after 
      ignoring trailing spaces) 
s/$/;/  add a semicolon at the end 
+0

除了'-i'不編輯到位的文件,但該文件用一個新的替換。 '-i'是最好的避免的非標準選項。 –

+0

我喜歡這個答案,因爲它解釋了正則表達式。謝謝! – wiredin

相關問題