Elisabeth
Elisabeth

Reputation: 343

awk - operation with choosen rows

I have this code:

 /^$/ { flag=0; next; }
/D Format/ { flag=0; next; }
/F Format/ { flag=1; next; }
/^26 / { next; }
flag && /^ *[0-9]/ { print t($5) "\n" t($6); }
function t(n, s) {
s=index(n,".");
return (s ? substr(n,1,s+6) : n);
}

that yields for this part of input file:

                      Input-Output in F Format

No.  Curve    Input Param.        Correction     Output Param.    Standard Deviation
 9      0     43.8999000000     -0.2148692026     43.6850307974      0.1066086900
10      0      0.0883000000     -0.0081173828      0.0801826172      0.0006755954
11      0      2.5816650000      0.1530838229      2.7347488229      0.0114687081
15      0      0.2175000000      0.0018561462      0.2193561462      0.0017699976
16      0     80.4198910000      3.4449399961     83.8648309961      0.1158732928
20      0      1.9424000000      0.3078499311      2.2502499311      0.0047924544
23      0      3.5047300000      0.4315780848      3.9363080848      0.0052905759
24      0      5.5942300000      1.8976306735      7.4918606735      0.0092102115
26      0  54804.4046000000     -0.0029799077  54804.4016200923      0.0006133608

Output is:

43.685030
0.106608
0.080182
0.000675
2.734748
0.011468
0.219356
0.001769
83.864830
0.115873
2.250249
0.004792
3.936308
0.005290
7.491860
0.009210

I would like to multiply numbers in row starting:

11 by 180 and devided by 3.1415, 
16 by 100
20 by 10.

How to write this arithmetic operation valid for only some rows? Something with NF? And the second question: How to write more input file? awk -f code input1 input2 input3 input4 .... or is some better way? Thank you

EDIT

 /^$/ { flag=0; next; }
/D Format/ { flag=0; next; }
/F Format/ { flag=1; next; }
/^ 9 / { print t($5) "\n" t($6);  } 
/^10 / { print t($5) "\n" t($6);  } 
/^11 / { print t($5*180/3.141592653589) "\n" t($6*180/3.141592653589);  }
/^15 / { print t($5*100) "\n" t($6*100);  }
/^16 / { print t($5) "\n" t($6);  } 
/^20 / { print t($5*10) "\n" t($6*10);  }
/^23 / { print t($5) "\n" t($6);  } 
/^24 / { print t($5) "\n" t($6);  }
function t(n, s) {
s=index(n,".");
return (s ? substr(n,1,s+6) : n);
}

Output

43.685030
0.106608
0.080182
0.000675
156.69
0.657109
21.9356
0.177
83.864830
0.115873
22.5025
0.047924
3.936308
0.005290
7.491860
0.009210
0.436850
0.106608
0.801826
0.675595
15.669
6.57109
21.9356
17.7
0.838648
0.115873
2.25025
4.79245
0.393630
0.529057
0.749186
0.921021

I would like to have 6 decimal places and the last number 0.009210

Upvotes: 0

Views: 155

Answers (1)

tomc
tomc

Reputation: 1207

for multiple files, they will just work as you indicate they should awk keeps two file row counters one for all the lines seen from every file NR and one for the number of rows seen in the current file FNR

So if in each file, the same row numbers need to be processed in a particular way:

including ... && FNR == X to your rule pattern will single out row X for special treatment.

It seem to me you have a pretty good grasp of the rest. However you may think you need to explicitly do something with each row but you can also not match a row and awk will just move on with out needing an explicit next

Upvotes: 2

Related Questions