Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

multiple commands inside a awk

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...