如何将多行打印为字段?

时间:2013-12-19 10:18:09

标签: perl awk

我的数据如下:

P04637 1A1U 1AIE 1C26 1DT7 1GZH 1H26 1HS5 1JSP 1KZY 1MA3 1OLG 1OLH 1PES 1PET 1SAE 1SAF 1SAK 1SAL 1TSR 1TUP 1UOL 1XQH 1YC5 1YCQ

但我想如下:

P04637 1A1U
P04637 1C26
P04637 1AIE
P04637 1DT7
P04637 1XQH
P04637 1MA3
P04637 1PES
P04637 1SAE

7 个答案:

答案 0 :(得分:4)

这将打印第一个字段,然后打印每个后续字段,直到该行结束。

$ awk '{for (i=2;i<=NF;i++) print $1,$i}' file                                 
P04637 1A1U
P04637 1AIE
P04637 1C26
P04637 1DT7
P04637 1GZH
P04637 1H26
P04637 1HS5
P04637 1JSP
P04637 1KZY
P04637 1MA3
P04637 1OLG
P04637 1OLH
P04637 1PES
P04637 1PET
P04637 1SAE
P04637 1SAF
P04637 1SAK
P04637 1SAL
P04637 1TSR
P04637 1TUP
P04637 1UOL
P04637 1XQH
P04637 1YC5
P04637 1YCQ

准确地获得OP请求,不多也不少:

awk '{print $1,$2 RS $1,$4 RS $1,$3 RS $1,$5 RS $1,$23 RS $1,$11 RS $1,$14 RS $1,$16}' file
P04637 1A1U
P04637 1C26
P04637 1AIE
P04637 1DT7
P04637 1XQH
P04637 1MA3
P04637 1PES
P04637 1SAE

答案 1 :(得分:2)

使用awk的另一种方法:

awk 'NR==1 {a=$1; next} {print a,$1}' RS=" " file

通过设置RS=" ",我们将行分隔符定义为空格。这样,我们每次都会获取不同的记录。

NR==1表示在阅读第一条记录时。在那里,我们存储第一个值。从那时起,我们继续写保存的值+当前的值。

它返回:

P04637 1A1U
P04637 1AIE
P04637 1C26
P04637 1DT7
P04637 1GZH
P04637 1H26
P04637 1HS5
P04637 1JSP
P04637 1KZY
P04637 1MA3
P04637 1OLG
P04637 1OLH
P04637 1PES
P04637 1PET
P04637 1SAE
P04637 1SAF
P04637 1SAK
P04637 1SAL
P04637 1TSR
P04637 1TUP
P04637 1UOL
P04637 1XQH
P04637 1YC5
P04637 1YCQ

答案 2 :(得分:2)

perl -lane '$v=shift @F; print "$v $_" for @F' file

答案 3 :(得分:1)

perl -lane 'print "$F[0] $_" for(@F[1..$#F])'

答案 4 :(得分:1)

这项工作不需要Perl和Awk。 Bash就足够了:

{ IFS=' ' read -r -a A ; for I in ${A[@]:1} ; do echo ${A[0]} $I ; done ; } <<<'P04637 1A1U 1AIE 1C26 1DT7 1GZH 1H26 1HS5 1JSP 1KZY 1MA3 1OLG 1OLH 1PES 1PET 1SAE 1SAF 1SAK 1SAL 1TSR 1TUP 1UOL 1XQH 1YC5 1YCQ'
P04637 1A1U
P04637 1AIE
P04637 1C26
...

您所需要的只是在这里:

答案 5 :(得分:0)

作为perl脚本:

use warnings;
use strict; 

open my $input, '<', 'in.txt' or die "$!";

while (<$input>){
    my @split = split;
    my $spacer = shift @split;
    print "$spacer $_\n" foreach @split;
}

打印:

P04637 1A1U
P04637 1AIE
P04637 1C26
P04637 1DT7
P04637 1GZH ... 

答案 6 :(得分:0)

刚才意识到原来的问题是要求随机出口

awk 'BEGIN{srand($RANDOM)}
{ split($0,a,FS);
  for (i=2;i<=NF;i++) 
     { while (1) 
         {  s=int(rand()*(NF-1)+2)
            if (a[s]!="") {print $1,a[s];delete a[s];break}
         }
     }
}' file

结果

P04637 1C26
P04637 1H26
P04637 1DT7
P04637 1HS5
P04637 1AIE
P04637 1GZH
P04637 1A1U