Reputation: 199
I have infile.txt file with multiple columns and rows like this:
infile.txt
2020 01 13 00 28.5833 77.2000 979 0 282.6 284.3 285.4 0 0
2020 01 13 00 28.5833 77.2000 925 469.578 290.4 296.9 297.7 3.6 5.1
2020 01 13 00 28.5833 77.2000 909 613.987 290.8 298.8 299.5 4.7 3.3
2020 01 13 00 28.5833 77.2000 850 1169.4 288 301.6 303.1 9.3 0
2020 01 13 00 28.5833 77.2000 700 2776.28 279 308.9 309.6 0 7.1
2020 01 13 00 28.5833 77.2000 500 5561.01 258.1 314.6 314.8 14.2 11.9
and, I want to perform some column-based calculation as follows:
awk '{R=0; if($12) R=(('$g'/'$theta_vs')*($11-'$theta_vs')*($8-'$z_s'))/(($12^2)+($13^2)); print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,R }' > test.txt
This work perfect and keep R=0 when $12!=0 but this provides an output value 0 when $12==0 as follows:
outputfile:
2020 01 13 00 28.5833 77.2000 979 0 282.6 284.3 285.4 0 0 0
2020 01 13 00 28.5833 77.2000 925 469.578 290.4 296.9 297.7 3.6 5.1 5.08926
2020 01 13 00 28.5833 77.2000 909 613.987 290.8 298.8 299.5 4.7 3.3 9.01363
2020 01 13 00 28.5833 77.2000 850 1169.4 288 301.6 303.1 9.3 0 8.21755
2020 01 13 00 28.5833 77.2000 700 2776.28 279 308.9 309.6 0 7.1 0
2020 01 13 00 28.5833 77.2000 500 5561.01 258.1 314.6 314.8 14.2 11.9 16.3555
I want to keep R=0 when both $12 && $13 is 0.
How can I make it?
Thank you
Upvotes: 0
Views: 222
Reputation: 133545
Could you please try following.
awk -v G="$g" -v theta="$theta_vs" -v z="$z_s" '{R=0; if($12 || $13) R=((G/theta)*($11-theta)*($8-z))/(($12^2)+($13^2)); print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,R }' Input_file
In case your lines have only 13 fields then as per Ed sir's suggestion adding following.
awk -v G="$g" -v theta="$theta_vs" -v z="$z_s" '{R=0; if($12 || $13) R=((G/theta)*($11-theta)*($8-z))/(($12^2)+($13^2)); print $0,R }' Input_file
Upvotes: 4
Reputation: 781370
Change
if ($12)
to
if ($12 || $13)
This will assign R
if either of them is non-zero, and leave it at 0
if both of them are zero.
Upvotes: 4