Crosstab to List table using awk Function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Crosstab to List table using awk Function
# 1  
Old 09-08-2009
Crosstab to List table using awk Function

What I am trying to achieve is turning crosstab into a normal table

e.g.
convert following table

Jan Feb Mar Apr May Jun
Australia 1 2 3 4 5 6
USA 7 8 9 10 11 12
China 13 14 15 16 17 18


to

Australia Jan 1
Australia Feb 2
Australia Mar 3
Australia Apr 4
Australia May 5
Australia Jun 6
USA Jan 7
USA Feb 8
USA Mar 9
USA Apr 10
USA May 11
USA Jun 12
China Jan 13
China Feb 14
China Mar 15
China Apr 16
China May 17
China Jun 18

I have been trying to come up with following awk code, but not quite getting the expected result.


awk '
BEGIN {FS = "\t" ;}
{
for ( NR >1 && x=2 ; x<=NF ; x++)
{
for (NR == 1 && a=3 ; a<=NF ; a++)
{
M[a]=$a
}
BC = $1
print BC , M[x], $x
}
}
' filename


If anybody tells a better way of doing it, I would greatly appreciate it.

Thank You !!

Last edited by asdban; 09-08-2009 at 07:57 PM..
# 2  
Old 09-09-2009
How about this:

Code:
awk '
NR==1{
	for(i=1;i<=NF;i++)
		head[i]=$i
}

NR>1{
	for(i=2;i<=NF;i++)
		print $1,head[i-1],$i
}' yourFile

# 3  
Old 09-10-2009
It worked
Thanks a lot !!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

2. UNIX for Dummies Questions & Answers

Crosstab

Good day everyone... I need help to do crosstab over my file input input Nation|Country|Capital|Name|Notes|Final|1/1/2014|1/2/2014|1/3/2014|1/4/2014|1/5/2014|1/6/2014|12/25/2014|12/26/2014|12/27/2014|12/28/2014|12/29/2014|12/30/2014|12/31/2014|12/26/2014 India|Calcuta|New... (3 Replies)
Discussion started by: radius
3 Replies

3. Shell Programming and Scripting

awk to convert table-by-row to matrix table

Hello, I need some help to reformat this table-by-row to matrix? infile: site1 A:o,p,q,r,s,t site1 C:y,u site1 T:v,w site1 -:x,z site2 A:p,r,t,v,w,z site2 C:u,y site2 G:q,s site2 -:o,x site3 A:o,q,s,t,u,z site3 C:y site3 T:v,w,x site3 -:p,routfile: SITE o p q r s t v u w x y... (7 Replies)
Discussion started by: yifangt
7 Replies

4. Shell Programming and Scripting

Build a table from a list by comparing existing table entries

I am new to this shell scripting.... I have a file which contains list of users. This files get updated when new user comes into the system. I want to create script which will give a table containing unique list of users. When I say unique, it means script should match table while parsing... (3 Replies)
Discussion started by: dchavan1901
3 Replies

5. Shell Programming and Scripting

Reformatting a list to table

Hi! I have a list with a lot of records that I need to work with. The problem is that the list is populated successive one record at the time in a text file, and to gain anything from these records I need them to be put out in a table. This is an example of what the list looks like: (145)... (5 Replies)
Discussion started by: ivar.friheim
5 Replies

6. UNIX for Dummies Questions & Answers

Reading Table name from a list of files in a Directory

Hi , I have searched through the forum but not able to find out any help :( i have a directory having lot of files which contains sql statemtns eg : file 1 contains select from table_name1 where ..................... select from table_name2 where .......... select from ... (3 Replies)
Discussion started by: Trendz
3 Replies

7. Shell Programming and Scripting

Read Table,View,Package,Function and Procedure Name in a File

Hi I am new to Unix shell scripting. But i need help to slove the below issue. Issue description: I want to read table, view names and package names in a file my plan to find the table name is : search "From" key word find the table or view To find the packge name : Search "Package... (5 Replies)
Discussion started by: sboss
5 Replies

8. Programming

c++ function to convert a linear list to circular list

hi all, i need a c++ function which converts a linear list to circular. presently i am working with two files. i.e., one linear list file. and one circular list file to do some operations. i thought it will be helpful if there is a function that converts a linear list to circular n undo the... (1 Reply)
Discussion started by: vidyaj
1 Replies

9. Shell Programming and Scripting

building table from list

Hi, I have a file with the following structure M17XX-050-01-001 1100000000 A16 1.341E+05 ... B18 3.084E+02 total 1.344E+05 XY35 5.694E+03 ... XY241 6.725E+02 total 9.897E+05 Wr81Z 5.195E+00 ... Wr91Z 1.029E+02 Wr92Z 1.285E+02 total 9.897E+05 M17XX-050-01-001 1010000000... (2 Replies)
Discussion started by: f_o_555
2 Replies

10. Programming

Sharing C++ Objects with virtual function table pointers

I am pondering the next question: Can I safely sare objects that have virtual functions (i.e. have virtual function table pointers) between two processes ? Where will the pointers point to in each process ? What I am afraid of is that in the creating process the pointer will indeed point to... (2 Replies)
Discussion started by: Seeker
2 Replies
Login or Register to Ask a Question