I have the following code in cmd.awk:
BEGIN {FS=","}
{
if(FNR==1) print $0",Header";
else if (FNR>1)
{
if($79==0 && $80==0 && $81==0) print $0",0";
else if ($80==0 && $81!=0) print $0","($79-$81)/$81;
else if ($81==0 && $80!=0) print $0","($79-$80)/$80;
else if ($81==0 && $80==0 && $79!=0) print $0",10";
else if ($81!=0 && $80!=0) print $0","(($79-$80)/$80)+(($80-$81)/$81);
}
}
When i execute the following commamnd:
awk -f cmd.awk input.txt
it performs the required operation(as specified in the AWK Script) and provides the required result.
But in this script all the columns of the input txt file are being accessed based on the column_index i.e., $79, $80, $81 etc.
My requirement is that i need to use this script as a function which takes $79, $80, $81 and Header(as given in the script) as parameters, performs operations and stores result in the newly appended column with column name Header and store the new contents into an output file. But i am only allowed to specify the parameters in the form of column headers and not in column index i.e., my function call has to be something like this:
cmd(column_header1, column_header2, column_header3,new_header)
and the function definition of cmd() has to perform the operation mentioned in the awk script above.
Is there any way to do this? Please bear in mind that I'm very new to awk. Thanks in advance.
My input file contains 150 columns and over 50M rows. A sample of the file is given below:
RN,DATE,ID,PRE_M1,PRE_M2,GALV,GALG,PRE_M5.........................TOTAL
0624873840,2016/04/28,201610,1618,0,0,0,Active,.................12234
0747269250,2016/02/02,201610,227,93,0,0,Daat,....................99988
The input file contains columns of type numeric,character. The columns being accessed in the above AWK script are all of type numeric.
A sample of the required output file is as below:
RN,DATE,ID,PRE_M1,PRE_M2,GALV,GALG,PRE_M5.........................TOTAL,Header
0624873840,2016/04/28,201610,1618,0,0,0,Active,.................12234,10
0747269250,2016/02/02,201610,227,93,0,0,Daat,....................99988,0
Please note that a new column is being appended to the file with name "Header" and this column contains the result of the AWK script for each individual row of the input file.