
我不小心在許多伺服器上向 /etc/crontab 分發了一行,我注意到該行中有一個拼寫錯誤,我需要在所有伺服器上更改它。
答案1
另一個選擇是修復拼字錯誤,而不僅僅是刪除最後一行
sed -i '$ s/typotext/correcttext/ /var/spool/cron/user
您應該能夠直接編輯較新版本的 cron 的 crontab 文件,因為它會立即檢查變更。
答案2
如果您的 sed 支援就地編輯,那麼,
sed -i '$d' file
如果沒有,您需要使用複合命令,例如,
cat file | sed '$d' > newfile; mv newfile file
答案3
使用head
user@host$ cat <<EOF > test.txt
1
2
3
4
EOF
user@host$ head -n -1 test.txt | sponge test.txt
user@host$ cat test.txt
1
2
3
user@host$
答案4
請參閱 unix.com 上的這篇論壇文章以獲取答案:
http://www.unix.com/shell-programming-scripting/25027-delete-last-line.html
sed -e '$d' 輸入檔 > 輸出文件
或者
頭 -$(( wc -l file | awk '{print $1}'
- 1)) 文件