Transpose Second column only


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Transpose Second column only
# 1  
Old 05-28-2014
Transpose Second column only

Hi Folks,

My input file is like this

Code:
cat input

abcd:efgh:jklm 123,456,67,78,89,90
hi:kil:op 76,78,12,3456,
unix:linux:shell:bash 111,111

My expected output

Code:
abcd:efgh:jklm hi:kil:op unix:linux:shell:bash 
123 76 111
456 78 111
67 12
78 3456
89
90

My trial

Code:
awk '{ for (i = 1; i <= NF; i++) f[i] = f[i] " " $i ; if (NF > n) n = NF } END { for (i = 1; i <= n; i++) sub(/^  */, "", f[i]) ;for (i = 1; i <= n; i++) print f[i] }' input

# 2  
Old 05-28-2014
Try
Code:
awk     'NF>n           {n=NF}
                        {for (i=1;i<=NF;i++) TMP[NR,i]=$i}
         END            {for (i=1;i<=n;i++)
                           {for (j=1; j<=NR; j++) printf "%s ", TMP [j,i]
                            printf "\n"
                           }
                        }
        ' FS="[ ,]" file
abcd:efgh:jklm hi:kil:op unix:linux:shell:bash 
123 76 111 
456 78 111 
67 12  
78 3456  
89   
90

This User Gave Thanks to RudiC For This Post:
# 3  
Old 05-28-2014
Quote:
Originally Posted by RudiC
Try
Code:
awk     'NF>n           {n=NF}
                        {for (i=1;i<=NF;i++) TMP[NR,i]=$i}
         END            {for (i=1;i<=n;i++)
                           {for (j=1; j<=NR; j++) printf "%s ", TMP [j,i]
                            printf "\n"
                           }
                        }
        ' FS="[ ,]" file
abcd:efgh:jklm hi:kil:op unix:linux:shell:bash 
123 76 111 
456 78 111 
67 12  
78 3456  
89   
90

Thanks Rudic. This code works perfect on the sample input.

But worked partially on my actual file.

The records are not being published to the output properly. I see varying number of records under the columns. For ex: instead of 2 rows under the third column in my sample output, I see only two.

Any pointers?

Last edited by jacobs.smith; 05-28-2014 at 12:59 PM.. Reason: Code working partially.
# 4  
Old 05-28-2014
Quote:
Any pointers?
Give us an accurate input file.


Also, if you have a problem it would be very helpful to:

Post the input lines that are not giving a correct output.
Post the output that it is not correct.
# 5  
Old 05-28-2014
Here is the actual input files first two lines

Code:
GO:0044699:single-organism_process:1.94262300468481e-19:7.12366338351597e-23:437:635:12657:25412:0.00%:0.00	DAF2,5830411N06RIK,TBX21,C1QL3,PFN2,GNAQ,EBI3,LRRK1,GPR133,ELAVL3,TNFRSF8,DTL,SRC,TUB,MEGF8,ALOX8,PRKAR1B,CYBB,DYNLT3,CXCR3,TTN,MTAP2,PDE3B,SRXN1,CYP2S1,CXCL2,ADAP1,ADSSL1,ST3GAL6,SGK3,FUCA2,MAGED1,PON3,DAB2IP,RAP1GAP,ARAP3,SLC22A15,MBOAT1,GUCY1B3,IRF5,PALLD,SLC5A5,ATP6V0D2,ACOT7,FGD6,FGR,SEMA6D,MYO6,COLQ,GAS2L1,IL17RB,ITGB3,CCL5,BHLHE40,HMGN3,BCAT1,NAPRT1,SAMSN1,SLC43A3,NELF,APP,TMEM38B,HPN,DST,DPT,TGFBR3,ALS2CL,TOP2A,CHSY1,SMPDL3B,PCYT1B,ACVRL1,PKDCC,CACNA1I,CYP4V3,LIMA1,CELA1,BCL2A1D,SLC12A8,KLF2,CD81,CC2D2A,ICA1,DUS2L,TRIM2,MGMT,SOSTDC1,GJB2,BCL2L2,MAN1C1,RGL1,GPM6B,GPR132,PDLIM7,IGFBP4,INPPL1,LTB4R1,MYO1D,ARHGAP29,CSDA,LEF1,RHOD,PRR5L,TNFSF14,CCDC40,PRDM5,AMPD1,NLRC4,CD9,ITGA7,DECR1,MRVI1,H2-OA,REPIN1,ANXA4,TNFRSF1B,GSTT3,ITGB5,AREG,NPAS4,CNGA1,SELENBP1,EPB4.1L1,ACPP,MYO1E,SLC25A13,MAPK8IP1,CD160,CD55,PRF1,ST8SIA1,GUCY2E,NAIP1,KLRG1,PLCB4,FAM101B,DDR1,SYNPO,SEMA4A,RIAN,GGT7,NAIP2,ITGAE,PIK3AP1,STXBP1,BSPRY,MIAT,NR1D1,KIF11,TNFSF10,HSPB1,CYFIP1,HSF4,GUCY1A3,LITAF,WISP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F1A,FABP5,CASP4,CD22,LAMA5,HAP1,DAPL1,PADI3,ALS2CR4,HPSE,SPOCK2,SIX5,CD38,ABCB1A,HIVEP3,NAIP7,RAB39B,NRN1,PRNP,DOCK4,RGS16,TMEM176A,DGAT2,CD79B,BC026585,NACC2,IFT81,GRHL1,CACNA1S,PLCL1,DAPK1,IFNG,ARHGEF5,DIAP3,HGF,PLXNB1,SCCPDH,PGLYRP1,AHR,APLP1,MAD1L1,AFP,TWSG1,LYN,WLS,NRBP2,NLGN2,TRIB2,EGR2,KLF7,MYL6B,Foxp3,SLC36A4,ATP9A,FUT10,LBP,RABL5,SLC16A3,NLRP1B,ABHD4,CCR8,CYSLTR2,BAIAP3,ENPP5,TRPM1,SWAP70,CHDH,ART3,ERGIC1,HDAC11,T2,BCL2A1A,MATN2,CSF1,SCG5,IL6RA,PASK,TBC1D4,CAMK2A,SLC2A6,C3,GGT5,PTPRS,INSRR,NRP2,F13A1,IL9R,RLN3,CD40LG,FCRL1,GRM6,TICAM2,CD83,AMOT,SERPINA3G,MAPRE3,SLC22A2,PIP5K1B,PLEKHG3,PGCP,SLC22A17,TRIM16,NPNT,AHI1,TMEM146,RAD51L1,RYR2,ICAM5,RNF43,RUNX2,ASB2
GO:0044763:single-organism_cellular_process:2.15608710343398e-19:7.90644335692695e-23:400:635:11146:25412:0.00%:0.00	5830411N06RIK,TBX21,C1QL3,PFN2,GNAQ,EBI3,LRRK1,GPR133,ELAVL3,TNFRSF8,DTL,SRC,TUB,MEGF8,ALOX8,CYBB,DYNLT3,CXCR3,TTN,MTAP2,PDE3B,CXCL2,ADAP1,ADSSL1,ST3GAL6,SGK3,MAGED1,PON3,DAB2IP,RAP1GAP,ARAP3,SLC22A15,MBOAT1,GUCY1B3,IRF5,PALLD,SLC5A5,ATP6V0D2,ACOT7,FGD6,FGR,SEMA6D,MYO6,COLQ,GAS2L1,IL17RB,ITGB3,CCL5,HMGN3,BCAT1,NAPRT1,SAMSN1,SLC43A3,NELF,APP,TMEM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 02:07 PM ---------- Previous update was at 12:44 PM ----------

I figured out the issue.

Here is the change that cleared my issue

Code:
awk     'NF>n           {n=NF}
                        {for (i=1;i<=NF;i++) TMP[NR,i]=$i}
         END            {for (i=1;i<=n;i++)
                           {for (j=1; j<=NR; j++) printf "%s\t", TMP [j,i]
                            printf "\n"
                           }
                        }
        ' FS="[ ,]" input

# 6  
Old 05-28-2014
The issue is that you have tabs embedded, as well, instead of only spaces

Change RudiC's code from FS="[ ,]" to FS="[ \t,]"

Perhaps an alternative

Code:
awk -F"[ \t,]" '{for (i=1; i<=NF; i++){a[i] != "" ? a[i]= a[i] (" "$i):a[i]=$i}} END {for (k in a) print a[k]}'

Check and see if any of that will help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Transpose the Row and column

Hi, I have data in form of A ram B shyam C seeta D geeta A bob B methew C Richad D Mike and i want it in this form. A B C D ram shyam seeta geeta bob methew Richard Mike. please help by providing the scripting for this. (3 Replies)
Discussion started by: ricbha
3 Replies

2. Shell Programming and Scripting

How transpose column in a row?

Hello guys, First of all happy holidays and happy new year. I'm new in bioinformatic and also it is my first time that I write in this forum. Therefore, sorry if I make some mistakes. I'm writing to ask your help to fix a problem: I have a file like this: gene1 GO:0016491|GO:0055114... (8 Replies)
Discussion started by: Salvatore_espos
8 Replies

3. Shell Programming and Scripting

Transpose from row to column using timestamp in first column

Gents, Transpose from row to column, taking in consideration the first column, which contends the date. Input file 72918,111000009,111000009,111000009,111000009,111000009,111000009,111000009,111000009,111000009 72918,2356,2357,2358,2359,2360,2361,2362,2363,2364 72918,0,0,0,0,0,0,0,0,0... (12 Replies)
Discussion started by: jiam912
12 Replies

4. Shell Programming and Scripting

Transpose row to column

I'm using the testawk.awk from the following thread https://www.unix.com/shell-programming-and-scripting/18897-row-column-transpose.htmlI'm getting the following output fieldname1 data1 fieldname2 data2 fieldname3 data3 How can I get like this instead 1 fieldname1 data1 2 fieldname2 data2... (1 Reply)
Discussion started by: makkan
1 Replies

5. Shell Programming and Scripting

Column to row Transpose

Hi Folks, Iam a kinda newbie to unix shell scripting, the scenario is i have a text file containing the following info Charlie chicago 15 Charlie newyork 26 jonny chicago 14 jonny newyork 15 joe chicago 15 joe newyork 18output should be Name chicago ... (3 Replies)
Discussion started by: tech_frk
3 Replies

6. UNIX for Dummies Questions & Answers

Row to column transpose

Hi there, Below is sample three rows which i need transpose into multiple rows. By keeping first 2 fields static and split them into multiple rows depend following date field. Each into seperate rows. Sample code: ... (6 Replies)
Discussion started by: ganeshd
6 Replies

7. Shell Programming and Scripting

Transpose from 2nd column till the last column

Hi I have 5 columns like this a b c d e f g h i j k l m n o From 2nd column till the 5th column of every record, I would like to transpose them as rows, so my output file contains only one row a b c d e f g h i j (9 Replies)
Discussion started by: jacobs.smith
9 Replies

8. Shell Programming and Scripting

Transpose field names from column headers to values in one column

Hi All, I'm looking for a script which can transpose field names from column headers to values in one column. for example, the input is: IDa;IDb;IDc;PARAM1;PARAM2;PARAM3; a;b;c;p1val;p2val;p3val; d;e;f;p4val;p5val;p6val; g;h;i;p7val;p8val;p9val; into the output like this: ... (6 Replies)
Discussion started by: popesk
6 Replies

9. Shell Programming and Scripting

Transpose column to row

Hi i have a file which has values seperated by "," as shown below and I want to transpose for every doc_id in one row. Input: DOC_ID,KEYWORD 105,REGISTROS 105,GEOLOGIA 105,NUCLEOS 105,EXPEDIENTE 105,PROGRAMAS 10025,EXPEDIENTE 10025,LOCALIZACIONES 10025,OFICIOS 10025,PROGRAMAS... (4 Replies)
Discussion started by: juelillo
4 Replies

10. Shell Programming and Scripting

Row to column transpose

Can we transpose rows to columns? Fields within row are separated by a comma. (10 Replies)
Discussion started by: videsh77
10 Replies
Login or Register to Ask a Question