简体   繁体   中英

Unix cut/ awk: Print same column multiple times (e.g. 1000 times)

How to copy a column for multiple times?

eg

Input

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

Output

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

Thanks!

使用简单的for循环并打印行要多少次:

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

Using awk , change variable n=<your_interest> value, according to your need.

One-liner:

 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

Input:

$ 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

When 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

Suppose if you set first column that is col=1 , then

$ 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

Better Readable:

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

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

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

It gets very easy in awk with the power of formatted strings.

For ex.

$ 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

You can modify it to count=999 for your desired output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM