Say I have a file:
PRO 1
GLN 5.55112e-17
ILE -6.245e-17
THR 5.55112e-17
I want every line that has a number unequal to 1 in the second column to change it to 0 and keep the rest.
If I use if
(i.e conditional statement), everything is OK:
awk '{if($2!=1){print $1,"0"}else{print $0}}' file
PRO 1
GLN 0
ILE 0
THR 0
But when I use the conditional block, something undesired happens:
awk '$2!=1 {print $1,"0"} {print $0}' file
PRO 1
GLN 0
GLN 5.55112e-17
ILE 0
ILE -6.245e-17
THR 0
THR 5.55112e-17
You can see what's wrong.
- How do I fix this error?
- Why does this error occur?
- What's the different between a conditional statement and a conditional block?
awk '$2!=1?$2=0:"";1' file
. – terdon Jul 10 '14 at 00:55next
. I guess it suppresses the second print if the first one is true. Something likecontinue
inC
. – Alexander Cska Jan 27 '17 at 11:00next
suppresses processing current input line, skip to the next one. The same role aswhile
, but for the wholeawk
program. Also,awk
has its ownwhile
– cuonglm Jan 27 '17 at 15:07