Diff

字元串和文件的區別

  • May 22, 2017

基本上我想檢查同一文件之前和之後的差異sed

試圖執行:

diff /opt/postTrades.sh <<< $(sed 's/1\ MIN/10\ MIN/g' /opt/postTrades.sh)

diff <<< $(sed 's/1\ MIN/10\ MIN/g' /opt/postTrades.sh) < /opt/postTrades.sh 

diff <<< (sed 's/1\ MIN/10\ MIN/g' /opt/postTrades.sh) < /opt/postTrades.sh

總是得到:

diff: missing operand after '/opt/postTrades.sh'
diff: Try 'diff --help' for more information.

正確的方法是什麼?謝謝。

diff需要兩個文件操作數。使用here-string,您可以將輸入傳遞到diff其標準輸入。

要從diff標準輸入中讀取,請-用作文件名:

$ diff file - <<<"$( sed 'expression' file )"

或者,更便攜,

$ sed 'expression' file | diff file -

<(...)如果您使用的 shell 支持使用、bash和進行程序替換ksh93,這可能是另一種解決方案:

$ diff file <( sed 'expression' file )

程序替換將顯示為一個文件,diff並且實際上“相同”:

$ sed 'expression' file >tempfile
$ diff file tempfile
$ rm tempfile

引用自:https://unix.stackexchange.com/questions/366506