Pivoting using shell scripts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pivoting using shell scripts
# 1  
Old 07-26-2016
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

Code:
 tx_id    tx_amt     typ_cd   fixed_dis_amt  
100     200            mc                     0.4
101     300            sc                       0.2

expecting output

Code:
 tx_id    tx_amt     mc                     sc 
100     200            0.4                    null
101     300            null                    0.2


Last edited by RudiC; 07-26-2016 at 01:05 PM.. Reason: Corrected code tags.
# 2  
Old 07-26-2016
Code:
awk '
NR==1 {t[tc++]=$1; t[tc++]=$2; next;}
{a[NR,t[0]]=$1; a[NR,t[1]]=$2; if (! pt[$3]) t[tc++]=$3; a[NR,$3]=$4; pt[$3]=$3}
END {
   $0="";
   for (i=0; i<tc; i++) $0=$0 t[i] ((i<tc) ? "\t" : "");
   print $0;
   for (i=2; i<=NR; i++) {
      $0="";
      for (j=0; j<tc; j++) {
         $0=$0 ((a[i,t[j]]) ? a[i,t[j]] : "null") ((j<tc) ? "\t" : "");
      }
      print $0;
   }
}
' input


Last edited by rdrtx1; 07-26-2016 at 01:47 PM..
# 3  
Old 07-26-2016
Try also, esp. with a larger file with several more columns,
Code:
awk '
NR==1           {HD = $1 OFS $2
                 HDC = 2
                 next
                }
!($3 in COLS)   {COLS[$3] = ++HDC
                 HD = HD OFS $3
                }
                {T[$1 OFS $2]++
                 VAL[$1 OFS $2 OFS $3] = $4
                }
END             {print HD
                 for (t in T)   {$0 = t
                                 for (c in COLS) $COLS[c] = VAL[t OFS c]?VAL[t OFS c]:"null"
                                 print
                                }
                }
' OFS="\t" file

# 4  
Old 07-27-2016
Input
Code:
[akshay@localhost tmp]$ cat f
 tx_id    tx_amt     typ_cd   fixed_dis_amt  
100     200            mc                     0.4
101     300            sc                       0.2

Script
Code:
[akshay@localhost tmp]$ cat test.awk
FNR==1{
	h = $1 OFS $2
}
FNR>1{
	A[$1,$2] = $1 OFS $2; B[$3]; C[$1,$2,$3] = $4
}
END{
	for(i in A)
	{
		f = ""
		for(j in B)
		{
			if(h)h = h OFS j
			ins = i SUBSEP j
			c = ins in C ? C[ins] : "null"
			f = f ? f OFS c : A[i] OFS c
		}
		if(h)
		{ 
			print h
			h = "" 
		}
		print f
	}
	
}

How to execute ?
Code:
[akshay@localhost tmp]$ awk -vOFS="\t" -f test.awk f

Output
Code:
tx_id	tx_amt	mc	sc
101	300	null	0.2
100	200	0.4	null

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

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

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

4. Shell Programming and Scripting

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 2012 Aug Canada 114 2012 Aug USA 92 2012 Aug Mexico 3 2012 Aug ... (3 Replies)
Discussion started by: karumudi7
3 Replies

5. Shell Programming and Scripting

Transposing rows and columns (pivoting) using shell scripting

Here is the contents of an input file. A,1,2,3,4 10,aaa,bbb,ccc,ddd 11,eee,fff,ggg,hhh 12,iii,jjj,lll,mmm 13,nnn,ooo,ppp I wanted the output to be A 10 1 aaa 10 2 bbb 10 3 ccc 10 4 ddd 11 1 eee 11 2 fff 11 3 ggg 11 4 hhh ..... and so on How to do it in ksh... (9 Replies)
Discussion started by: ksatish89
9 Replies

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

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

8. Shell Programming and Scripting

calling 'n' number of shell scripts based on dependency in one shell script.

Hello gurus, I have three korn shell script 3.1, 3.2, 3.3. I would like to call three shell script in one shell script. i m looking for something like this call 3.1; If 3.1 = "complete" then call 3.2; if 3.2 = ''COMPlete" then call 3.3; else exit The... (1 Reply)
Discussion started by: shashi369
1 Replies

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

10. AIX

Difference between writing Unix Shell script and AIX Shell Scripts

Hi, Please give me the detailed Differences between writing Unix Shell script and AIX Shell Scripts. Thanks in advance..... (0 Replies)
Discussion started by: haroonec
0 Replies
Login or Register to Ask a Question