Transpose the data

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Transpose the data
# 1  
Old 10-14-2016
Transpose the data

Hi All,

I have sort of a case to transpose data from rows to column
input data

Code:
Afghanistan|10000|1
Albania|25000|4
Algeria|25000|7
Andorra|10000|4
Angola|25000|47
Antigua and Barbuda|25000|23
Argentina|5000|3
Armenia|100000|12
Aruba|20000|2
Australia|50000|2

I need to transpose data based on 2nd column, so my output should be like

Code:
Nation|5000|10000|20000|25000|50000|100000
Afghanistan|0|1|0|0|0|0
Albania|0|0|0|4|0|0
Algeria|0|0|0|7|0|0
Andorra|0|4|0|0|0|0
Angola|0|0|0|47|0|0
Antigua and Barbuda|0|0|0|23|0|0
Argentina|3|0|0|0|0|0
Armenia|0|0|0|0|0|12
Aruba|0|0|2|0|0|0
Australia|0|0|0|0|2|0

I did this
Code:
awk 'NR==1 {split ($0, D)} NR>1 {for (i=2; i<=NF; i++) print $1,$2, D[i], $i} ' FS="|" OFS="|" input

Please help

Last edited by radius; 10-14-2016 at 12:51 AM..
# 2  
Old 10-14-2016
You might want to try something more like:
Code:
awk '
BEGIN {	# Set input and output field separators.
	FS = OFS = "|"
}
{	# For each line read from the input file(s)...
	# Gather data.
	d[$1,$2] = $3
	# If we have not seen this nation before...
	if(!($1 in nation)) {
		# ... save the new nation and add it to the list specifying the
		# output order.
		nation[$1]
		nation_order[++nationc] = $1
	}
	# If we have not seen this value before...
	if(!($2 in values)) {
		# ... save the new value and increment the number of values we
		# have seen.
		values[$2]
		valuesc++
	}
}
END {	# After we have gathered data from all of the input file(s), sort the
	# values we have seen into increasing order while setting the field
	# headers.
	headers[++headersc] = "Nation"
	# While we have a value left in the values[] array...
	while(valuesc--) {
		# ... find the smallest value left in values[].
		m = 1E20
		for(j in values) {
			if(j + 0 < m)
				m = j + 0
		}
		# Set the next output field header to the lowest value found,
		# increment the number of header fields, and delete the
		# minimum value from the values[] array.
		headers[++headersc] = m
		delete values[m]
	}
	# Print the header line.
	for(i = 1; i <= headersc; i++)
		printf("%s%s", headers[i], (i < headersc) ? OFS : ORS)
	# Print the data line for each nation in our list of nations...
	for(i = 1; i <= nationc; i++) {
		# ... print the nation...
		printf("%s%s", nation_order[i], OFS)
		for(j = 2; j <= headersc; j++)
			# ... and print the data values for this nation.
			printf("%s%s", d[nation_order[i], headers[j]] + 0,
			    (j < headersc) ? OFS : ORS)
	}
}' input	# End the awk script and name the file to be processed.

which, if input contains the sample data you provided in post #1, produces the output:
Code:
Nation|5000|10000|20000|25000|50000|100000
Afghanistan|0|1|0|0|0|0
Albania|0|0|0|4|0|0
Algeria|0|0|0|7|0|0
Andorra|0|4|0|0|0|0
Angola|0|0|0|47|0|0
Antigua and Barbuda|0|0|0|23|0|0
Argentina|3|0|0|0|0|0
Armenia|0|0|0|0|0|12
Aruba|0|0|2|0|0|0
Australia|0|0|0|0|2|0

as requested.

If you want to try this code on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.

Last edited by Don Cragun; 10-17-2016 at 05:12 AM.. Reason: Fix typo: s/and and/and add/
# 3  
Old 10-17-2016
it works, thanks for the explanation SmilieSmilieSmilieSmilieSmilie
# 4  
Old 10-17-2016
Quote:
Originally Posted by radius
thanks for the explanation
You might want to use the button labeled "Thanks" in the lower left corner of every posting to express your thanks to Don.

bakunin
This User Gave Thanks to bakunin For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Transpose large data in UNIX

Hi I have the following sample of data: my full data dimention is 900,000* 1119 rs987435 C G 1 1 1 0 2 rs345783 C G 0 0 1 0 0 rs955894 G T 1 1 2 2 1 rs6088791 ... (7 Replies)
Discussion started by: marwah
7 Replies

2. UNIX for Advanced & Expert Users

Transpose Messy Data

I have a messy, pipe-delimited ("|") input dataset. I would like to create a file of ID plus each component of field 4 which is delimited by ";" into a long, skinny shape for easier processing. A couple of complications are that field 4 may contain both commas and linefeed characters from the... (9 Replies)
Discussion started by: 91674io
9 Replies

3. Shell Programming and Scripting

Help with transpose data content

Hi, Below is my input file: c116_g1_i1 -,-,-,+ c118_g2_i1 +,+ c118_g3_i1 + c120_g1_i1 +,+,+,+ . . Desired Output File c116_g1_i1 - c116_g1_i1 - c116_g1_i1 - c116_g1_i1 + c118_g2_i1 + c118_g2_i1 + (3 Replies)
Discussion started by: perl_beginner
3 Replies

4. Shell Programming and Scripting

Transpose data as rows using awk

Hi I have below requirement, need help One file contains the meta data information and other file would have the data, match the column from file1 and with file2 and extract corresponding column value and display in another file File1: CUSTTYPECD COSTCENTER FNAME LNAME SERVICELVL ... (1 Reply)
Discussion started by: ravlapo
1 Replies

5. Shell Programming and Scripting

Transpose Column of Data to Rows

I can no longer find my commands, but I use to be able to transpose data with common fields from a single column to rows using a command line. My data is separated as follows: NAME=BOB ADDRESS=COLORADO PET=CAT NAME=SUSAN ADDRESS=TEXAS PET=BIRD NAME=TOM ADDRESS=UTAH PET=DOG I would... (7 Replies)
Discussion started by: docdave78
7 Replies

6. Shell Programming and Scripting

Transpose Data from Columns to rows

Hello. very new to shell scripting and would like to know if anyone could help me. I have data thats being pulled into a txt file and currently have to manually transpose the data which is taking a long time to do. here is what the data looks like. Server1 -- Date -- Other -- value... (7 Replies)
Discussion started by: Mikes88
7 Replies

7. Shell Programming and Scripting

Transpose Daily Data from Column to Row.

Hi I'm looking to transpose Linux data from a daily report that logs every 10mins like below. After the first "comma" I need the daily total for Col2 and Col3 transposed like below. The new transposed format below will then be exported to Microsoft Excel for Reporting. Any help would be... (9 Replies)
Discussion started by: ravzter
9 Replies

8. Shell Programming and Scripting

Transpose columns to Rows : Big data

Hi, I did read a few posts on the subjects, tried out a few solutions, but did not solve my problem. https://www.unix.com/302121568-post11.html https://www.unix.com/shell-programming-scripting/137953-large-file-columns-into-rows-etc-4.html Please help. Problem very similar to the second link... (15 Replies)
Discussion started by: genehunter
15 Replies

9. Shell Programming and Scripting

How to transpose a table of data using awk

Hi. I have this data below:- v1 28 14 1.72414 1.72414 1.72414 1.72414 1.72414 v2 77 7 7.47126 6.89655 6.89655 6.89655 6.89655 v3 156 3 21.2644 21.2644 20.6897 21.2644 20.6897 v4 39 3 1.72414 1.72414 1.72414 1.72414 1.72414 v5 155 1 21.2644 23.5632 24.1379 23.5632 24.1379 v6 62 2 2.87356... (2 Replies)
Discussion started by: ahjiefreak
2 Replies

10. Shell Programming and Scripting

How to transpose data elements in awk

Hi, I have an input data file :- Test4599,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,2,2,Rain Test90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,0,Not Rain etc.... I wanted to transpose these data to:-... (2 Replies)
Discussion started by: ahjiefreak
2 Replies
Login or Register to Ask a Question