1. 程式人生 > >實現兩個檔案內容逐行進行對比的shell指令碼

實現兩個檔案內容逐行進行對比的shell指令碼

寫一個實現兩個檔案內容逐行進行對比的shell指令碼,將兩個檔案相同的內容輸出到一個檔案中。

程式碼實現如下:

#!/bin/bash
#output is the same line in file1 andfile2

for line1 in $(cat $1)
    do
        grep $line1 $2 > /dev/null
            if [ $? -eq 0 ]; then
               echo "${line1}" >> samefile.txt
            fi
    done

執行指令碼:

[email protected]:~$ ./compileFile.sh ./file_1.txt ./file_2.txt

執行完後,會在指令碼存放同目錄下生成一個名稱為samefile.txt的檔案,這個檔案中就儲存了file_1.txt和file_2.txt相同的內容。