0

I have the file like this...

a|b|c|d|e|1|2|3|4|5|f|g|h

I need the result as below

a|b|c|d|e|1|f|g|h  
a|b|c|d|e|2|f|g|h  
a|b|c|d|e|3|f|g|h  
a|b|c|d|e|4|f|g|h  
a|b|c|d|e|5|f|g|h  

Basically I am trying to transpose 5 columns, with the rest of the columns repeated for each of those 5 attributes... (here values 1,2,3,4,5... in single row is being transposed into into 5 separate rows... in all those 5 rows, a,b,c,d,e,f,g,h are duplicated.

Please help using AWK/SED/CUT anything.

0

awk solution:

awk -F'|' '{ for(i=6;i<=10;i++) print $1,$2,$3,$4,$5,$i,$11,$12,$13 }' OFS='|' file

The output:

a|b|c|d|e|1|f|g|h
a|b|c|d|e|2|f|g|h
a|b|c|d|e|3|f|g|h
a|b|c|d|e|4|f|g|h
a|b|c|d|e|5|f|g|h

  • for(i=6;i<=10;i++) - iterating through the crucial fields
  • This worked ! Thank you very much ! However, I have 200 columns like this... it looks ugly to go on "print $1,$2... ,$200", is there a easy way to put it in a short form ? – Ramesh Lakshman Jun 28 '17 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.