天天看点

awk 比较两个文件的异同

 # cat file1

module1 1.1 123

module2 2.1 123

module11

module12

 #cat file2

module3 3.1 123

module21

module22

# ./compare.sh

module1 1.1 same

module3 3.1 different

module21 new  in file2

module22 new  in file2

module2 new  in file1

module11 new  in file1

module12 new  in file1

 #cat compare.sh

#!/bin/bash

#Compare two files and if $1 and $2 are same, print

awk 'NR==FNR{a[$1]=$2} NR>FNR &&a[$1]&&($2==a[$1]) { print $1,$2,"same"}' file1 file2

#Compare two files and if $2 are different , print

awk 'NR==FNR{a[$1]=$2} NR>FNR &&a[$1]&&($2!=a[$1]) { print $1,$2,"different" }' file1 file2

#Compare two files and if $1 and $2 are same, print,didn't consider blank lines

awk 'NR==FNR{a[$1]=$2} NR>FNR && (!a[$1]) { print $1,"new  in file2" }' file1 file2

awk 'NR==FNR{a[$1]=$2} NR>FNR && (!a[$1]) { print $1,"new  in file1" }' file2 file1

继续阅读