0

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
7
  • 3
    awk is a separate programming language, not part of bash. And bash has a printf but it's completely separate from awk's printf. There's a lot that could be done to make this clearer. Commented Apr 7, 2021 at 16:21
  • @CharlesDuffy thanks I'll edit the title Commented Apr 7, 2021 at 16:21
  • BTW, echo $output is itself inherently buggy (of particular note it'll eat your newlines and print all your output on the same line, even when you've gotten your code fixed to the point where awk is writing two separate lines of output); always echo "$output" instead -- see I just assigned a variable, but echo $variable prints something different! Commented Apr 7, 2021 at 16:22
  • @CharlesDuffy agreed. I fixed it Commented Apr 7, 2021 at 16:23
  • And awk can easily do that in single command without any bash directive Commented Apr 7, 2021 at 16:25

2 Answers 2

1
$ 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
Sign up to request clarification or add additional context in comments.

3 Comments

this works great. I was able to fit this inside my bash script. Thanks for your help
instead of $2 == "=" how can I call anything between 2 commas instead of $2 column
Sorry, I don't know what that means (I can imagine several things it might mean) and chameleon questions are strongly discouraged anyway so please just ask a new followup question.
0

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

1 Comment

Thank you for the sed option however in my case I will have different operators. file1 might have != while file 2 might have >. You can see the criteria in the awk command

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.