AWK replace one field

I have some problem with this.
Need to change field #3 to 5 if field #1 = A and filed #2 =B

Data

A C 6 T
C B 4 R
A B 3 T
D E 5 4

I would like to do two things if statement is true, but can not get it to work.

Here it prints column #3 if statement is true, and this works

awk '{if ($1=="A" && $2=="B") print $3; else print $0}'

Here is what I have tested:

 awk '{if ($1=="A" && $2=="B") $3=5 print $0; else print $0}'
awk '{if ($1=="A" && $2=="B") $3=5; print $0; else print $0}'
awk '{if ($1=="A" && $2=="B") ($3=5 print $0); else print $0}'
awk '{if ($1=="A" && $2=="B") ($3=5; print $0); else print $0}'

All gives error.
I know this can be done in other ways, but I like to learn how to make to things in one if

$ awk '$1=="A" && $2=="B" { $3 = 5 } 1' file1
A C 6 T
C B 4 R
A B 5 T
D E 5 4

What was the second thing?

print it :slight_smile:
You made a nice simple solution, thanks.

You were almost there in your above fourth attempt. Just exchange the () with {} and it works:

$ awk '{if ($1=="A" && $2=="B") {$3=5; print $0} else print $0}' file
A C 6 T
C B 4 R
A B 5 T
D E 5 4

Of course, there's some room for improvements...

awk '/^A B/ {$3=5} 1' file