比較兩個文件中,一個文件比另一個文件多的行
阿新 • • 發佈:2017-09-20
fun logs use 比較 nth $1 then author func
1. 該腳本用來比較兩個文件中,其中一個文件比另一個文件多的行,常用來工作環境中,對比得出多余的ip地址
#!/bin/bash #different in file1 and file2 #author:vaedit #date:2017/8/20 #read -p "請輸入第一個文件路徑" file1 #read -p "請輸入第二個文件路徑" file2 function print_help(){ echo "該腳本只用來對比一個文件比另一個文件多出的行的內容" echo "useage -f file1 file2" } function grepfile(){ if [ -f $file1 -a -f $file2 ]then FILE1_LENTH=`wc -l $file1 | awk ‘{print $1}‘` FILE2_LENTH=`wc -l $file2 | awk ‘{print $1}‘` if [ "$FILE1_LENTH" -gt "$FILE2_LENTH" ] then echo "$file1 中有,$file2 中沒有的行如下" echo -e "\e[32;40;1m===========================================\e[0m" echo "===========================================" grep -vwf "$file2" "$file1" echo "===========================================" echo -e "\e[32;40;1m===========================================\e[0m" elseecho "$file2 中有,$file1 中沒有的行如下" echo -e "\e[32;40;1m===========================================\e[0m" echo "===========================================" grep -vwf "$file1" "$file2" echo "===========================================" echo -e "\e[32;40;1m===========================================\e[0m" fi else echo "請輸入正確的文件路徑" exit 1 fi } while test -n "$1";do case "$1" in -h| --help) print_help exit ;; -f| --file) file1="$2" file2="$3" grepfile shift 3 ;; *) echo "===========================================" print_help exit esac done
比較兩個文件中,一個文件比另一個文件多的行