multiple commands inside a awk

China☆狼群 提交于 2020-01-07 09:23:03

问题


sry opening a new thread but awk is driving me nuts! >< im trying to run a few command assignments inside a single awk but i cant get it to work please help if this is ez for u :P i can't get the syntax to work

edit: im using /bin/bash

for f in `seq $nlinpE $loopE`; 
do 

awk  -F ","'  
BEGIN {}    
'$f'  { dataI2[$f]=$2;
    dataI3[$f]=$3;
    dataI4[$f]=$4;
    noD1[$f]=$dataI1[$f];
    noD2[$f]=$dataI2[$f];
    noD3[$f]=$dataI3[$f];
    noD1i[$f]=`echo "$nlinpN1 + $dataI1"|bc -l`;
    noD2i[$f]=`echo "$nlinpN1 + $dataI2"|bc -l`;
    noD3i[$f]=`echo "$nlinpN1 + $dataI3"|bc -l`;
    }   
'${noD1i[$f]}' { 
    dataIi2[$f]=$2;
    dataIi3[$f]=$3;
    dataIi4[$f]=$4;
    }

'${noD2i[$f]}'  { 
    dataIii2[$f]=$2;
    dataIii3[$f]=$3;
    dataIii4[$f]=$4;
    }

'${noD2i[$f]}'  { 
    dataIiii2[$f]=$2;
    dataIiii3[$f]=$3;
    dataIiii4[$f]=$4;
    }
END{}                 
' <aoa_5.inp;

done

input is like:

 17,   3.22854114,  0.562598288,  0.384291202
 18,   2.96085286,  0.085116826,  0.285071939
 19,   3.40070796,   2.27838659,  0.302027524
 20,   3.20035744,  0.333615214,  0.262585849
 21,   2.85644341,  0.258691043,  0.369726121
 22,   3.73537922,    1.3267405,  0.295917094
 23,   3.69372559,   1.32601321,  0.306054831
 24,   3.28857207,   0.63199228,  0.378117412
 25,   3.27523994,  0.695856452,  0.377585977

imjust assigning variables atm, getting the number w/o commas
i get this syntax type of error:

awk: 9: unexpected character '`'
awk: 10: unexpected character '`'
awk: 11: unexpected character '`'
(standard_in) 2: syntax error
(standard_in) 2: syntax error
awk: line 1: syntax error at or near {
^C

thx


回答1:


Maybe this will help you clean up your syntax a little so we can understand what you're trying to do.

BEGIN and END blocks are optional. Ignoring patterns for the moment, an awk program might look like this.

BEGIN {
    # Things to be done before you start processing rows.
}
{
    # Things to be done for each row.
}
END {
    # Things to be done after processing the last row.
}

If you don't happen to need BEGIN or END blocks, it might look more like this.

{
    # Things to be done for each row.
}

This awk program assigns the value of $2, $3, and $4 to the variable dataI, and prints it once for each row.

{
  dataI = sprintf("%s %s %s", $2, $3, $4);
  print dataI;
}

That assignment has no effect on the values of $2, $3, and $4.



来源:https://stackoverflow.com/questions/8302642/multiple-commands-inside-a-awk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!