-1

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 
8
  • Perl or Awk.. which one? Commented Dec 19, 2013 at 10:20
  • What have you tried? What problems are you having? StackOverflow is not a "write my code for me" service. Commented Dec 19, 2013 at 10:58
  • 1
    @DaveCross - I can't understand the up votes for this... No effort made and an initially incomprehensible non-question Commented Dec 19, 2013 at 11:01
  • how to export column 2? Seems they are not in original order. Commented Dec 19, 2013 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. Commented Dec 19, 2013 at 12:30

7 Answers 7

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 
Sign up to request clarification or add additional context in comments.

4 Comments

classic solution.. +1
Hi, all, just realize the original question is asking to export randomly, see my solution. :-)
How can you tell that form the OPs text. He may have written it wrong. All line is not listed in output request, so he may only need some. Than all solution here would be wrong.
@BMW Se my updated post.
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 

3 Comments

nice one +1. I wrote one with setting OFS, but leave an empty line. this is better.
Hi, all, just realize the original question is asking to export randomly, see my solution. :-)
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.
2
perl -lane '$v=shift @F; print "$v $_" for @F' file 

Comments

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

Comments

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:

Comments

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

2 Comments

He does not asked for tabulators.
@ceving - he doesn't really ask for anything.
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 

4 Comments

For random order awk '{for(i=2;i<=NF;i++)print $1,$i}' file | sort -R
Why do you use the number 14? And $RANDOM will be equal to the empty string "" I think..
you are right, I have updated to NF-1. I did a test on 14 items before
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.