Pivoting the data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pivoting the data
# 1  
Old 11-27-2012
IBM Pivoting the data

Hello Unix guys,

I have the following 4 column data, which is a output of db2 select query:

Need to pivot the data.

sample Input:
Year Month Country Counts
Code:
       2012 Aug    Canada        114
       2012 Aug    USA           92
       2012 Aug    Mexico        3
       2012 Aug    Aus           20
       2012 Sep    India         158
       2012 Sep    Canada        106
       2012 Sep    USA           89
       2012 Sep    Mexico        3
       2012 Sep    Aus           13
       2012 Oct    India         1571953
       2012 Oct    Canada        1387428
       2012 Oct    USA           1330861
       2012 Oct    Mexico        75663
       2012 Oct    Aus           143765
       2012 Oct    Alpha         1
       2012 Nov    India         391
       2012 Nov    Canada        657
       2012 Nov    USA           894
       2012 Nov    Mexico        360
       2012 Nov    Aus           1034

Desired Output:
Code:
        2012 Aug      2012 Sep    2012 Oct     2012 Nov
India      178           158        1571953         391
Canada  114            106        1387428         657
USA       92              89        1330861          894
Mexico    3               3          75663            360
Aus        22              13        143765         1034
Alpha      0                0         1                    0

Can you please help in this.

Thanks.

Last edited by karumudi7; 11-27-2012 at 08:45 AM..
# 2  
Old 11-27-2012
Try sth like this


Code:
awk '{if(!X[$1,$2]++){FIR=FIR?FIR"_"$1" "$2:$1" "$2}
{Y[$1,$2,$3]=$4;Z[$3]++}}END{n=split(FIR,FCL,"_");for(i=1;i<=n;i++){s=s?s"\t"FCL[i]:"\t"FCL[i]}
print s;
for(j in Z){s=j;for(i=1;i<=n;i++){split(FCL[i],PK," ");p=Y[PK[1],PK[2],j]?Y[PK[1],PK[2],j]:0;s=s"\t\t"p};print s}}' file

This User Gave Thanks to pamu For This Post:
# 3  
Old 11-27-2012
Quote:
Originally Posted by pamu
Try sth like this


Code:
awk '{if(!X[$1,$2]++){FIR=FIR?FIR"_"$1" "$2:$1" "$2}
{Y[$1,$2,$3]=$4;Z[$3]++}}END{n=split(FIR,FCL,"_");for(i=1;i<=n;i++){s=s?s"\t"FCL[i]:"\t"FCL[i]}
print s;
for(j in Z){s=j;for(i=1;i<=n;i++){split(FCL[i],PK," ");p=Y[PK[1],PK[2],j]?Y[PK[1],PK[2],j]:0;s=s"\t\t"p};print s}}' file

Excellent it worked.

If possible can you please explain this code.
# 4  
Old 11-28-2012
Quote:
Originally Posted by karumudi7
If possible can you please explain this code.

Code:
awk '{if(!X[$1,$2]++){ # Here we remove duplicates of date as $1 and $2. So it will take only single entry for any month.

FIR=FIR?FIR"_"$1" "$2:$1" "$2}  # Here we append date $1 and $2 to variable FIR using FS as _.


{Y[$1,$2,$3]=$4;Z[$3]++}} # Here we Assign $4 to array using index as $1,$2,$3 and add country's to array Z. In array Z only unique countries will be added.

END{n=split(FIR,FCL,"_"); # Here we split FIR var(all unique dates) to variable FCL using FS as _ and n = number dates present in FIR variable.

for(i=1;i<=n;i++){s=s?s"\t"FCL[i]:"\t"FCL[i]} # Here we just append all the unique months to the variable s.

print s;  # print variable s on first line.

for(j in Z){s=j;  # Get values from array Z and assign it to s. array Z contains unique country names

for(i=1;i<=n;i++){  # Use for loop using variable n(which we got from spiting variable FIR)

split(FCL[i],PK," ") # Split variable FCL[i](which is has year and month) to array PK.

p=Y[PK[1],PK[2],j]?Y[PK[1],PK[2],j]:0   # Here we check array Y presents or not. PK[1] = Year, PK[2] = Month and j = Country. If for that particular field we have array value then assign it to variable s and if not then assign it to 0

s=s"\t\t"p};  # then append it variable s

print s   # Print variable s

}}' file # Read file

I hope this helpsSmilie

pamu
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Pivoting data based on a header field

Hi Team, Could you please help me with the below scenario. I have a file which is in the below format. Zipcode,001,001f,002,002f,003,003f,004,004f,005,005f,006,006f,007,007f 0050, ,0, ,0, ,0, ,1,*,7, ,7, ,7 0060, ,0, ,0, ,7, ,0,*,7, ,0, ,0 Would need the output as below. First field... (1 Reply)
Discussion started by: saj
1 Replies

2. UNIX for Beginners Questions & Answers

Pivoting with awk

My input file(inputfile.txt): Job name.... EXTDPL_1 Created on.. 2018-11-19 14:00:00 Modified on. 2018-11-22 11:54:46 Job name.... EXTDPL_2 Created on.. 2018-11-21 12:31:27 Modified on. 2018-11-21 12:35:28 2 records listed. >Q expected output: Job name Created... (3 Replies)
Discussion started by: JSKOBS
3 Replies

3. UNIX for Beginners Questions & Answers

Pivoting values from column to rows

I/P: I/P: 2017/01/01 a 10 2017/01/01 b 20 2017/01/01 c 40 2017/02/01 a 10 2017/02/01 b 20 2017/02/01 c 30 O/P: a b c 2017/01/01 10 20 40 2017/02/01 10 20 30 (18 Replies)
Discussion started by: Booo
18 Replies

4. Homework & Coursework Questions

Help with pivoting table shell script

input file txt file 2000 1 name 2000 2 addr 2000 3 phone 2000 4 email 1000 1 name 1000 2 addr 1000 3 phone 1000 4 email 3000 1 name 3000 2 addr 3000 ... (4 Replies)
Discussion started by: senmatrix
4 Replies

5. Shell Programming and Scripting

Pivoting using shell scripts

Hi , Please any one help using shell scripts achieve the below output(pivoting on top_cd i mean type code values will come as individual columns and fixed amount is value for that .Any help would be greate tx_id tx_amt typ_cd fixed_dis_amt 100 200 mc ... (3 Replies)
Discussion started by: mohan705
3 Replies

6. Linux

Pivoting data with awk

Hi Friends, I need to pivot data . Below is my source data Source Data PK PRTY_KEY_ID PRTY_SUB_KEY_ID KEY_COL_VAL_TX MTCH_CNFDNCE_RATE 007824822 428844791 1 #Jemmy#Pom#600 Kearsarge Way 100 007824822 429283974 1 #Jemmy#Pom#120 Broadway 100 007824822 429739103 1 #Jemmy#Pom#600 Keae Way#757... (0 Replies)
Discussion started by: patiljeevan3
0 Replies

7. Shell Programming and Scripting

Vertical And Horizontal Pivoting

Hi All, My Input data is: A=1 B=2 My desired Output should be: A|B 1|2 Thanks in advance... (3 Replies)
Discussion started by: kmsekhar
3 Replies

8. Shell Programming and Scripting

Help with pivoting

Hi, I need help on how to pivot the data in UNIX. I have one input file in which The facts(FACT1,FACT2..) and PERIOD(JAN,FEB..) are columns.I need to pivot the data. For Exampe The input file and output file looks like below. Could you please help with this using awk:). INPUT: ====== ... (1 Reply)
Discussion started by: Gayathricheruku
1 Replies

9. Shell Programming and Scripting

Pivoting Dynamic rows into columns

Original file we are getting ....... Item Period Amt P1 106 1000 P1 206 1500 P1 106 2000 P2 256 5800 P2 650 7500 My output should be like this Item 106 206 256 650 ............ P1 1000 1500 0 ... (1 Reply)
Discussion started by: dprakash
1 Replies

10. UNIX for Dummies Questions & Answers

Pivoting a Single column

Hi, Input ID|Name 1|a,b,c 2|d,e,f,g I would like to get output in the following format. Output ID|NAME 1|a 1|b 1|c 2|d 2|e 2|f 2|g (2 Replies)
Discussion started by: deepakwins
2 Replies
Login or Register to Ask a Question