Cross Tab


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cross Tab
# 1  
Old 04-18-2016
Cross Tab

I need help to do cross tab

file input
Code:
20160101|ASIA|CHINA|2000
20160101|ASIA|INDIA|3000
20160102|ASIA|CHINA|4000
20160103|ASIA|CHINA|2000
20160103|AFRIKA|ZAMBIA|2000
20160104|ASIA|CHINA|5000

expected output
Code:
CONTINENT|NATION|20160101|20160102|20160103|20160104
ASIA|CHINA|2000|4000|2000|5000
ASIA|INDIA|3000|0|0|0
AFRIKA|ZAMBIA|0|0|2000|0

I did this
Code:
awk '
{
	for(i=1;i<=NF;i++)
		print $1,head[i-1],$i
}' input | awk -F'|' '{print ""CONTINENT""|""NATION""|"$0

# 2  
Old 04-18-2016
You might want to try something more like:
Code:
awk '
BEGIN {	FS = OFS = "|"
}
{	# Gather data:
	if(!($1 in dates))
		out_field[dates[$1] = ++nd] = $1
	continent_nation[$2 OFS $3]
	data[$2 OFS $3, $1] += $4
}
END {	# Print header line:
	printf("%s", "CONTINENT" OFS "NATION")
	for(i = 1; i <= nd; i++)
		printf("%s", OFS out_field[i])
	print ""

	# Print accumualted data lines:
	for(loc in continent_nation) {
		printf("%s", loc OFS)
		for(i = 1; i <= nd; i++)
			printf("%s%s", data[loc, out_field[i]] + 0,
			    (i < nd) ? OFS : ORS)
	}
}' input

which produces output similar to:
Code:
CONTINENT|NATION|20160101|20160102|20160103|20160104
ASIA|CHINA|2000|4000|2000|5000
AFRIKA|ZAMBIA|0|0|2000|0
ASIA|INDIA|3000|0|0|0

with your sample input file (although the order of the output lines after the header may vary with different versions of awk).

If you want to use this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Programming

what is a cross compiler

recently i was been to an interview to an automotive company, they have posed me a question that what section of compiler must be changed if the target is changed. ie,if ur compiler is meant for generating executable to a 8051 target what changes do you do to make it flexible to generate the... (2 Replies)
Discussion started by: shyam.sunder91
2 Replies

2. Linux

Cross compile

Hi, I'm cross compiling libelektra to arm from linux system its giving library errors can any one help me? (0 Replies)
Discussion started by: Gautham.P
0 Replies

3. Programming

Cross target debugging

I am trying to setup gdbserver debugging on my i386 target platform and running gdb from x86_64 host. I always get could not load vsyscal try using file etc.. error. Is there a clean way to setup a cross target gdb. PS: I also tried set archi i386 on host side. (2 Replies)
Discussion started by: dragonpoint
2 Replies

4. Shell Programming and Scripting

Cross checking two files

i have two files which contains some fields separated by columns as 03/29/1999 08:48:12 02 172.16.114.50 03/29/1999 09:08:00 480 172.16.112.100 Both of the files do have the same format I want the script which will take two such... (3 Replies)
Discussion started by: vaibhavkorde
3 Replies

5. HP-UX

nfs cross mount

how can I do nfs cross mount between two servers' nfs filesystems? should I put a wrapper or do I have to use /etc/rmtab ? advise me which method I have to use ? (2 Replies)
Discussion started by: xramm
2 Replies

6. Shell Programming and Scripting

files cross over

I have many files (File 1, File 2, File 3, ...) in same directory. The format of all these files are same. What I would like to do is to combine all these files into a single file via cross over. Example) >>File 1 look like this. f1 01 1.0 f1 02 2.0 f1 03 3.0 f1 04 4.0 f1 05 5.0 f1 06... (2 Replies)
Discussion started by: Jae
2 Replies

7. UNIX for Advanced & Expert Users

Cross platform Authentication

I am looking to have UNIX authenticate against Active Directory in a Windows Server 2003 environment, any suggestion? I am very new to UNIX, 2 weeks worth knowledge, if that. Thanks! (3 Replies)
Discussion started by: Optik
3 Replies

8. Post Here to Contact Site Administrators and Moderators

Cross Posting

In regards to this post: https://www.unix.com/showthread.php?s=&threadid=10372 it may be advisable to inform new members about the repercussions of cross-posting. (9 Replies)
Discussion started by: Karma
9 Replies
Login or Register to Ask a Question