开发者

How to print every 4th column up to nth column and from (n+1)th column to last using awk?

I have a dataset with 119 columns separated by a space and like to select columns (3,7,11,...,47,51), (52,53,54,...,118,119) and print them out to another file. How can I do this using awk?

Input file 

c1 c2 c3...c119

Output file

c3 c7 c11 ... c51 c52 c53 c54 ... c118 c119 
开发者_高级运维

Thanks for your help


awk '{for(i=3;i<=51;i+=4) printf "%s ",$i ;for(i=52;i<=119;i++) {printf "%s ",$i} ;print ""}' file


{a=""; 
 for (i=0 ;i<=12; i++) {a = a  $(3+4 * i) " "};
 for (i=52 ;i<=119; i++) {a = a  $(i) " "};
 print a}  

HTH!


Is this what you're looking for?

awk '{s=""; for (i = 3; i <= 51; i+=4) {s = s $i " "}; for (i = 52; i <= 119; i++) {s = s $i " "}; print s}' inputfile


awk 'for(i=3;i<=119;i+a) 
     {
     if (i<52)
     {
     printf "%s ",$i};
     a=4;
     }
     else
     {
     printf "%s ",$i};
     a=1;
     }
     }' file
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜