Unix cut / awk:多次打印同一列(例如1000次)

时间:2017-10-09 10:36:58

标签: linux unix awk cut

如何多次复制列?

e.g。

输入

1   4771131 4772199 ENSMUSG00000103922  0   +   0.670011
1   4773206 4785739 ENSMUSG00000033845  0   -   95.0352
1   4778063 4779212 ENSMUSG00000102275  0   -   0.1806
1   4807788 4848410 ENSMUSG00000025903  0   +   110.078

输出

1   4771131 4772199 ENSMUSG00000103922  0   +   0.670011    0.670011 x 998 times
1   4773206 4785739 ENSMUSG00000033845  0   -   95.0352 95.0352 x 998 times
1   4778063 4779212 ENSMUSG00000102275  0   -   0.1806  0.1806 x 998 times
1   4807788 4848410 ENSMUSG00000025903  0   +   110.078 110.078 x 998 times

谢谢!

4 个答案:

答案 0 :(得分:1)

使用简单的for循环并打印线条您想要的次数:

awk '{printf $0;for(i=1;i<=998;i++){printf("%s%s",$NF,i==998?"":" ")};print ""}' Input_file

答案 1 :(得分:1)

使用awk,根据需要更改变量n=<your_interest>值。

<强>一衬垫:

 awk -v col=1 -v n=2 'function repeat(v, n,i){for(i=1; i<=n; i++)printf("%s%s",(i==1?"":OFS),v)}{for(i=1; i<=NF; i++)printf("%s%s",(i==col?repeat($i,n):$i),i==NF?RS:OFS)}' infile

<强>输入:

$ cat infile
1   4771131 4772199 ENSMUSG00000103922  0   +   0.670011
1   4773206 4785739 ENSMUSG00000033845  0   -   95.0352
1   4778063 4779212 ENSMUSG00000102275  0   -   0.1806
1   4807788 4848410 ENSMUSG00000025903  0   +   110.078
  

col=7 and v=5

$ awk -v col=7 -v n=5 'function repeat(v, n,i){for(i=1; i<=n; i++)printf("%s%s",(i==1?"":OFS),v)}{for(i=1; i<=NF; i++)printf("%s%s",(i==col?repeat($i,n):$i),i==NF?RS:OFS)}' infile
1 4771131 4772199 ENSMUSG00000103922 0 + 0.670011 0.670011 0.670011 0.670011 0.670011
1 4773206 4785739 ENSMUSG00000033845 0 - 95.0352 95.0352 95.0352 95.0352 95.0352
1 4778063 4779212 ENSMUSG00000102275 0 - 0.1806 0.1806 0.1806 0.1806 0.1806
1 4807788 4848410 ENSMUSG00000025903 0 + 110.078 110.078 110.078 110.078 110.078
  

假设您设置的第一列是col=1,那么

$ awk -v col=1 -v n=5 'function repeat(v, n,i){for(i=1; i<=n; i++)printf("%s%s",(i==1?"":OFS),v)}{for(i=1; i<=NF; i++)printf("%s%s",(i==col?repeat($i,n):$i),i==NF?RS:OFS)}' infile
1 1 1 1 1 4771131 4772199 ENSMUSG00000103922 0 + 0.670011
1 1 1 1 1 4773206 4785739 ENSMUSG00000033845 0 - 95.0352
1 1 1 1 1 4778063 4779212 ENSMUSG00000102275 0 - 0.1806
1 1 1 1 1 4807788 4848410 ENSMUSG00000025903 0 + 110.078

更好的可读性:

awk -v col=7 -v n=5 '
                     function repeat(v, n,i)
                     {
                       for(i=1; i<=n; i++)
                            printf("%s%s",(i==1?"":OFS),v)
                     }
                     {
                       for(i=1; i<=NF; i++)
                         printf("%s%s",(i==col?repeat($i,n):$i),i==NF?RS:OFS)
                     }
                   ' infile

答案 2 :(得分:1)

Awk 解决方案(单print次操作):

awk '{ n=998;r=$NF; while(--n) r=r FS $NF; print $0,r}' OFS='\t' file

答案 3 :(得分:1)

凭借格式化字符串的强大功能, awk 非常容易。

对于前。

$ awk -v count=3 '{s=sprintf("%0*s",count,""); gsub(/ /," "$NF,s); printf $0 s "\n"}' file
1   4771131 4772199 ENSMUSG00000103922  0   +   0.670011 0.670011 0.670011 0.670011
1   4773206 4785739 ENSMUSG00000033845  0   -   95.0352 95.0352 95.0352 95.0352
1   4778063 4779212 ENSMUSG00000102275  0   -   0.1806 0.1806 0.1806 0.1806
1   4807788 4848410 ENSMUSG00000025903  0   +   110.078 110.078 110.078 110.078

您可以将其修改为count=999以获得所需的输出。