如何打印每第4列到第n列,从第(n + 1)列打印到最后使用awk?

时间:2011-02-27 03:19:27

标签: awk

我有一个数据集,其中119列用空格分隔,喜欢选择列(3,7,11,...,47,51),(52,53,54,...,118,119)并打印他们到另一个文件。我怎么能用awk做到这一点?

Input file 

c1 c2 c3...c119

Output file

c3 c7 c11 ... c51 c52 c53 c54 ... c118 c119 

感谢您的帮助

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

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

HTH!

答案 2 :(得分:0)

这是你要找的吗?

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

答案 3 :(得分:0)

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