0

My data as following:

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

But I want as following:

P04637 1A1U
P04637 1C26
P04637 1AIE
P04637 1DT7
P04637 1XQH
P04637 1MA3
P04637 1PES
P04637 1SAE
Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
  • Perl or Awk.. which one? – Jonathon Reinhart Dec 19 '13 at 10:20
  • What have you tried? What problems are you having? StackOverflow is not a "write my code for me" service. – Dave Cross Dec 19 '13 at 10:58
  • 1
    @DaveCross - I can't understand the up votes for this... No effort made and an initially incomprehensible non-question – fugu Dec 19 '13 at 11:01
  • how to export column 2? Seems they are not in original order. – BMW Dec 19 '13 at 11:21
  • @ceving: You should upvote questions if they are *well-phrased* and likely to be *highly relevant* to other users. Upvoting because a question gave you an entertaining puzzle breaks the system. – Borodin Dec 19 '13 at 12:30
  • That is a very poor rule of thumb for posting on Stack Overflow, or any social site for that matter. Moderators have exist because people like you get entertainment from being destructive. Please stop it. – Borodin Dec 19 '13 at 12:39
  • @ceving: If you mouse over the upvote message then its purpose is clear. *"This question shows reseach effort; it is useful and clear"*. Not *"I like this question cos I had fun with it"*. – Borodin Dec 19 '13 at 15:08
  • @Borodin Having fun soliloquizing? – ceving Dec 19 '13 at 22:10

7 Answers7

4

This prints the first field and then every subsequent field until end of the line.

$ 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

To get exactly what OP request, no more, no less:

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
Jotne
  • 40,548
  • 12
  • 51
  • 55
2

Another approach with awk:

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

By setting RS=" " we define the line separator as space. This way, we will fetch every time a different record.

NR==1 means while reading the first record. There, we store the first value. From that point, we keep writing the saved value + current one.

It returns:

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
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • nice one +1. I wrote one with setting `OFS`, but leave an empty line. this is better. – Kent Dec 19 '13 at 10:37
  • Hi, all, just realize the original question is asking to export randomly, see my solution. :-) – BMW Dec 19 '13 at 11:16
  • 2
    Well @BMW I wouldn't say randomly. I guess the sample output is just that, a sample, to have the idea. Some clarification from the author could help. – fedorqui Dec 19 '13 at 11:18
2
perl -lane '$v=shift @F; print "$v $_" for @F' file
mpapec
  • 50,217
  • 8
  • 67
  • 127
1
perl -lane 'print "$F[0] $_" for(@F[1..$#F])'
Vijay
  • 65,327
  • 90
  • 227
  • 319
1

Neither Perl nor Awk is required for this job. Bash is enough:

{ 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
...

All you need is already here:

Community
  • 1
  • 1
ceving
  • 21,900
  • 13
  • 104
  • 178
0

As a perl script:

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;
}

Prints:

P04637 1A1U
P04637 1AIE
P04637 1C26
P04637 1DT7
P04637 1GZH ... 
fugu
  • 6,417
  • 5
  • 40
  • 75
0

Just realize the original question is asking to export randomly

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

result

P04637 1C26
P04637 1H26
P04637 1DT7
P04637 1HS5
P04637 1AIE
P04637 1GZH
P04637 1A1U
BMW
  • 42,880
  • 12
  • 99
  • 116
  • 1
    For random order `awk '{for(i=2;i<=NF;i++)print $1,$i}' file | sort -R` – Chris Seymour Dec 19 '13 at 11:22
  • Why do you use the number `14`? And `$RANDOM` will be equal to the empty string `""` I think.. – Håkon Hægland Dec 19 '13 at 11:27
  • you are right, I have updated to NF-1. I did a test on 14 items before – BMW Dec 19 '13 at 11:41
  • this forum is interesting with bad attitude, people gave wrong answer, get votes, who challenge them but got vote down. This is still a free place, and I will keep challenge them without fear. – BMW Dec 19 '13 at 23:47