Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

Shell 輸出重定向實戰:find、sed 與檔案內容替換

把輸出導向檔案

將某個指令的所有輸出(正確與錯誤)都存到桌面檔案:

Shell 輸出重定向實戰:find、sed 與檔案內容替換

只保存正確輸出到 /mnt/find.out.1,同時丟棄錯誤輸出:

Shell 輸出重定向實戰:find、sed 與檔案內容替換

建立 /mnt/find.all 並保存全部輸出:

[student@localhost /]$ find /etd -name passwd &>> /mnt/find.all

再次寫入同一檔案並保留原內容(用 >> 追加):

Shell 輸出重定向實戰:find、sed 與檔案內容替換

錯誤輸出導到 /mnt/find.err:

Shell 輸出重定向實戰:find、sed 與檔案內容替換

正確輸出導到 /mnt/find.out:

Shell 輸出重定向實戰:find、sed 與檔案內容替換

只顯示正確輸出、屏蔽錯誤輸出:

Shell 輸出重定向實戰:find、sed 與檔案內容替換

只看見正確結果、把錯誤丟掉:

Shell 輸出重定向實戰:find、sed 與檔案內容替換

用 sed 做批次替換

找出 /usr/share/mime/packages/freedesktop.org.xml 中含有 ich 的行,存到 /root/lines;接著把該檔案行首的空白替換掉,並保留檔案原有內容:

sed -i 's/^\ */vim/g' /root/lines

-i 表示直接改寫原檔,^ 錨定行首,\ * 匹配零個以上空白。若只想預覽而不動原檔,把 -i 拿掉即可。

Copyright © Linux教程網 All Rights Reserved