Help newbie: transposing column into row (pivot)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help newbie: transposing column into row (pivot)
# 1  
Old 04-26-2012
Help newbie: transposing column into row (pivot)

Hi,

I have a file in this format.

Code:
Name | organization
John | INT
Abby| DOM
John | DOM
John | MIX
Jason | INT
Anna | DOM
Abby |MIX

I want the output to look this.

Code:
Name | organization
John | INT, DOM, MIX
Abby | DOM, MIX
Jason | INT
Anna | DOM

Please let me know if there is a easy way to do it?

Thanks

Last edited by Scrutinizer; 04-26-2012 at 02:26 PM.. Reason: Please use code tags
# 2  
Old 04-26-2012
Code:
$ awk -F"|" 'NR==1{ print; next};
        { A[$1]=A[$1] ", " $2 }
        END { for(X in A) {
                         print X "|" substr(A[X], 2);
                 } }' datafile

Name | organization
Jason |  INT
Abby|  DOM
Abby | MIX
John |  INT,  DOM,  MIX
Anna |  DOM

$

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 04-26-2012
Code:
awk -F\| '{
   if (NR>1) {
        if (x[$1]) x[$1] = x[$1] "," $2
        else {y[NR] = $1; x[$1] = "|"$2}
   } else print
} END {
  for (i=2;i<=NR;i++)
      if (y[i] in x) print y[i] x[y[i]]
}' file

This User Gave Thanks to shamrock For This Post:
# 4  
Old 04-26-2012
Thank Shamrock and Corona688.
I have another question, how do you prevent duplicate organization?

For an example, I don't want this

John | INT, DOM, MIX, MIX

Thanks again for your help.
# 5  
Old 04-27-2012
Quote:
Originally Posted by sirrtuan
Thank Shamrock and Corona688.
I have another question, how do you prevent duplicate organization?

For an example, I don't want this

John | INT, DOM, MIX, MIX

Thanks again for your help.
In that case try this awk program...
Code:
awk -F\| '{
   if (NR>1) {
      if (!z[$1,$2]++)
         x[$1] = x[$1] ? x[$1] "," $2 : "|" $2
   } else print
} END {for (i in x) print i x[i]}' file

# 6  
Old 04-27-2012
Code:
# awk -F'[|]' '{
if(NR==1)print;else{gsub(" ","",$1);a[$1]=a[$1]","$2}}END{for(i in a){
sub(",","|",a[i]);gsub(", *",", ",a[i])if(length(a[i])<15)print i,a[i]}
}' infile
Name | organization
Anna | DOM
Abby | DOM, MIX
Jason | INT

regards
ygemici
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print first row of column a, last row of column b if column a has the same value

I have a table with this structure: cola colb colc 1 19 lemon 20 31 lemon 32 100 lemon 159 205 cherries 210 500 cherries and need to parse it into this format: cola colb colc 1 100 lemon 159 500 cherries So I need the first row of cola and the last row of colb if colc has the... (3 Replies)
Discussion started by: coppuca
3 Replies

2. Shell Programming and Scripting

Transposing data based on 1st column

I do have a big tab delimited file of the following format aa 344 456 aa 34 67 bb 34 90 bb 23 100 bb 1 89 d 0 12 e 45 678 e 78 90 e 56 90 .... .... .... I would like to transpose the data based on the category on column one and get the output file in the following tab delimited... (8 Replies)
Discussion started by: Kanja
8 Replies

3. Shell Programming and Scripting

Pivot Column using awk

Hello everyone I have a 20M file which is having the below sample layout 1111,ABC,100 1111,DEF,200 1111,XYZ,300 4444,LMN,100 4444,AKH,500 4444,WCD,400 2222,ABC,100 7777,DEF,300 7777,WCD,300 I need to covert this to below format Output Party_ID|ABC|AKH|DEF|LMN|WCD|XYZ... (5 Replies)
Discussion started by: morbid_angel
5 Replies

4. Shell Programming and Scripting

Print row on 4th column to all row

Dear All, I have input : SEG901 5173 9005 5740 SEG902 5227 5284 SEG903 5284 5346 SEG904 5346 9010 SEG905 5400 5456 SEG906 5456 5511 SEG907 5511 9011 SEG908 5572 9015 SEG909 5622 9020 SEG910 5678 5739 SEG911 5739 5796 SEG912 5796 9025 ... (3 Replies)
Discussion started by: attila
3 Replies

5. Shell Programming and Scripting

Awk/sed script for transposing any number of rows with header row

Greetings! I have been trying to find out a way to take a CSV file with a large number of rows, and a very large number of columns (in the thousands) and convert the rows to a single column of data, where the first row is a header representing the attribute name and the subsequent series of... (3 Replies)
Discussion started by: tntelle
3 Replies

6. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

7. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

8. Shell Programming and Scripting

Moving data from a specified column/row to another column/row

Hello, I have an input file like the following: 11_3_4 2_1_35 3_15__ _16989 Where '_' is a space. The data is in a table. Is there a way for the program to prompt the user for x1,y1 and x2,y2, where x1,y1 is the desired number (for example x=6 y=4 is a value of 4) and move to a desired spot... (2 Replies)
Discussion started by: jl487
2 Replies

9. Shell Programming and Scripting

Transposing column to row, joining with another file, then sorting columns

Hello! I am very new to Linux and I do not know where to begin... I have a column with >64,000 elements (that are not in numberical order) like this: name 2 5 9 . . . 64,000 I would like to transpose this column into a row that will later become the header of a very large file... (2 Replies)
Discussion started by: doobedoo
2 Replies

10. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies
Login or Register to Ask a Question