Reputation: 579
I have two files $A and $B
$A contains (the operator varies, it could be =, > or !=) Basically I have the awk command working as it should I just want to add the line from $A where it failed
number1 = 460
number2 = 12
number3 > 5
number4 > 20
number5 != 39
number6 != 0
$B contains
number1 453
number2 12
number3 7
number4 19
number5 39
number6 4
I have an awk command that compares two files and tells me if the numbers don't match
output=`awk '
{
getline buf <f2;
split( buf, a, " " );
if( $1 == a[1] && $2 == ">" && $3+0 > a[2]+0 )
printf( "%s\n", buf );
else if( $1 == a[1] && $2 == "!=" && $3+0 == a[2]+0 )
printf( "%s\n", buf );
else if( $1 == a[1] && $2 == "=" && $3+0 != a[2]+0 )
printf( "%s\n", buf );
}
' f2="$B" $A`
echo "$output"
number1 453
number4 19
number5 39
I am trying to get this output:
echo "$output"
This is the line that failed: number1 = 460 #coming from $A
This is the correct number: number1 453 #coming from $B
This is the line that failed: number4 > 20 #coming from $A
This is the correct number: number4 19 #coming from $B
This is the line that failed: number5 != 39 #coming from $A
This is the correct number: number5 39 #coming from $B
Upvotes: 0
Views: 1330
Reputation: 203899
$ cat tst.awk
NR==FNR {
map[$1] = $2+0
next
}
$1 in map {
succ = 0
if ( ( ($2 == "=" ) && (map[$1] == $3) ) \
|| ( ($2 == "!=") && (map[$1] != $3) ) \
|| ( ($2 == ">" ) && (map[$1] > $3) ) \
|| ( ($2 == ">=") && (map[$1] >= $3) ) \
|| ( ($2 == "<" ) && (map[$1] < $3) ) \
|| ( ($2 == "<=") && (map[$1] <= $3) ) \
) {
succ = 1
}
if ( !succ ) {
printf "This is the line that failed: %s #coming from %s\n", $0, FILENAME
printf "This is the correct number: %s %s #coming from %s\n", $1, map[$1], ARGV[1]
print ""
}
}
$ awk -f tst.awk B A
This is the line that failed: number1 = 460 #coming from A
This is the correct number: number1 453 #coming from B
This is the line that failed: number4 > 20 #coming from A
This is the correct number: number4 19 #coming from B
This is the line that failed: number5 != 39 #coming from A
This is the correct number: number5 39 #coming from B
Upvotes: 1
Reputation: 535
As I prefer to use sed
over awk
, there is something:
diff -y file1 file2 |
sed 's/^\(.*\) = *\([0-9]\+\)[ \o11]\+|[ \o11]\+\1/\1 :: \2 !/p;d'
number1 :: 460 ! = 453
You could arrange:
diff -y file? |
sed 's/^\(.*\) = *\([0-9]\+\)[ \o11]\+|[ \o11]\+\1 = *\([0-9]\+\)/This is the line that failed: \1 = \2 # coming from file1\nThis is the correct number: \1 = \3 # comming from file2/p;d'
This is the line that failed: number1 = 460 # coming from file1
This is the correct number: number1 = 453 # comming from file2
Upvotes: 0