Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Transpose rows to certain columns Post 303030380 by Don Cragun on Friday 8th of February 2019 02:18:06 PM
Old 02-08-2019
Hi rahman.ahmed,
Welcome to the UNIX.com forums.

When starting a new thread it always helps if you tell us what operating system and shell you're using. Various utilities on different systems have options that vary form operating system to operating system and shells are notorious for offering different sets of features above and beyond those that are in almost all shells. If we know what operating system and shell you're using, we are less likely to make suggestions that won't work in your environment.

We are here to help you learn how to do things like this on your own; not to act as your unpaid programming staff. If you show us what you have tried to solve a problem on your own (posting your code in CODE tags) along with the sample input that you provided (please surround sample input data in CODE tags) and the output you hope to produce from that data (also, please in CODE tags) we can make better suggestions to help you get around whatever was keeping you from your goal.

In a case like this one might try something like:
Code:
awk -v Headers="Box Name,Weight,Length,Depth,Color" '
BEGIN {	# Split the Headers given to us into field names and print the output
	# header line.  Set NHdrs to the number of fields to be printed in each
	# output record.  Set elements of the subscript[] array to have the 
	# output field name as a subscript and the output field position as the
	# value.
	NHdrs = split(Headers, Hdrs, /,/)
	for(i = 1; i <= NHdrs; i++) {
		subscript[Hdrs[i]] = i
		printf("%s%s", Hdrs[i], (i == NHdrs) ? ORS : ",")
	}
	# Since the input field separator is sometimes <colon><space> and
	# sometimes <space><colon>, we will use a <colon> surrounded by any
	# number of <space>s as our field separator:
	FS = " *: *"
}

# Define a function to print an output record if any input data has been seen
# since we last printed a record.
function print_record(	loop) {
	if(data) {
		data = 0
		for(loop = 1; loop <= NHdrs; loop++)
			printf("%s%s", field[loop], (loop == NHdrs) ? ORS : ",")
		split("", field)
	}
}

NF {	# We have an input field on this line, gather the data and skip to the
	# next input record.
	field[subscript[$1]] = $2
	data++
	next
}

{	# Since there is no input fields on this line, print the data
	# accumulated from the previous lines as an output record.
	print_record()
}

END {	# In case there was no empty line at the end of the input file, also
	# print the data accumulated from the previous lines when we hit EOF.
	print_record()
}' file

These 2 Users Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rows to Columns - File Transpose

Hi I have an input file and I want to transpose it but I need to take care that if any field is missing for a record it should be popoulated with space for that field - using a shell script INFILE ---------- emp=1 sal=2 loc=abc emp=2 sal=21 sal=22 loc=xyz emp=5 loc=abc OUTFILE... (10 Replies)
Discussion started by: 46019
10 Replies

2. Shell Programming and Scripting

Transpose multipe columns to rows and adding headers

Hi, I found the following awk script to transpose multiple (3) columns to multiple rows: #=== BEGIN {FS=","} { for (i=1;i<=NF;i++) { arr=$i; if(nf<= NF) nf=NF; } nr=NR } END { for(i=1;i<=nf;i++) { (8 Replies)
Discussion started by: Gery
8 Replies

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

4. Shell Programming and Scripting

Transpose Rows Into Columns

I'm aware there are a lot of resources dedicated to the question of transposing rows and columns, but I'm a total newbie at this and the task appears to be beyond me. I have 40 text files with content that looks like this: Dokument 1 von 146 Orange County Register (California) June 26, 2010... (2 Replies)
Discussion started by: spindoctor
2 Replies

5. Shell Programming and Scripting

Transpose columns to Rows

I have a data A 1 B 2 C 3 D 4 E 5 i would like to change the data A B C D E 1 2 3 4 5 Pls suggest how we can do it in UNIX. Start using code tags, thanks. Also start reading your PM's you get from Mods as well read the Forum Rules. That might not do any harm. (24 Replies)
Discussion started by: aravindj80
24 Replies

6. Shell Programming and Scripting

transpose rows to columns

Any tips on how I can awk the input data to display the desired output per below? Thanking you in advance. input test data: 2 2010-02-16 10:00:00 111111111111 bytes 99999999999 bytes 90% 4 2010-02-16 12:00:00 333333333333 bytes 77777777777 bytes 88% 5 2010-02-16 11:00:00... (4 Replies)
Discussion started by: ux4me
4 Replies

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

8. Shell Programming and Scripting

Columns to Rows - Transpose - Special Condition

Hi Friends, Hope all is well. I have an input file like this a gene1 10 b gene1 2 c gene2 20 c gene3 10 d gene4 5 e gene5 6 Steps to reach output. 1. Print unique values of column1 as column of the matrix, which will be a b c (5 Replies)
Discussion started by: jacobs.smith
5 Replies

9. Shell Programming and Scripting

awk to transpose every 7 rows into columns

input: a1 a2 a3 a4 a5 a6 a7 b1 b2 b3 .. b7 .. z1 .. z7 (12 Replies)
Discussion started by: ux4me
12 Replies

10. Shell Programming and Scripting

Transpose rows to columns complex

Input: IN,A,1 IN,B,3 IN,B,2 IN,C,7 BR,A,1 BR,A,5 BR,C,9 AR,C,9 Output: CNTRY,A,B,C IN,1,5,7 BR,6,0,9 AR,0,0,9 (7 Replies)
Discussion started by: unme
7 Replies
stfttp.f(3)							      LAPACK							       stfttp.f(3)

NAME
stfttp.f - SYNOPSIS
Functions/Subroutines subroutine stfttp (TRANSR, UPLO, N, ARF, AP, INFO) STFTTP Function/Subroutine Documentation subroutine stfttp (characterTRANSR, characterUPLO, integerN, real, dimension( 0: * )ARF, real, dimension( 0: * )AP, integerINFO) STFTTP Purpose: STFTTP copies a triangular matrix A from rectangular full packed format (TF) to standard packed format (TP). Parameters: TRANSR TRANSR is CHARACTER*1 = 'N': ARF is in Normal format; = 'T': ARF is in Transpose format; UPLO UPLO is CHARACTER*1 = 'U': A is upper triangular; = 'L': A is lower triangular. N N is INTEGER The order of the matrix A. N >= 0. ARF ARF is REAL array, dimension ( N*(N+1)/2 ), On entry, the upper or lower triangular matrix A stored in RFP format. For a further discussion see Notes below. AP AP is REAL array, dimension ( N*(N+1)/2 ), On exit, the upper or lower triangular matrix A, packed columnwise in a linear array. The j-th column of A is stored in the array AP as follows: if UPLO = 'U', AP(i + (j-1)*j/2) = A(i,j) for 1<=i<=j; if UPLO = 'L', AP(i + (j-1)*(2n-j)/2) = A(i,j) for j<=i<=n. INFO INFO is INTEGER = 0: successful exit < 0: if INFO = -i, the i-th argument had an illegal value Author: Univ. of Tennessee Univ. of California Berkeley Univ. of Colorado Denver NAG Ltd. Date: November 2011 Further Details: We first consider Rectangular Full Packed (RFP) Format when N is even. We give an example where N = 6. AP is Upper AP is Lower 00 01 02 03 04 05 00 11 12 13 14 15 10 11 22 23 24 25 20 21 22 33 34 35 30 31 32 33 44 45 40 41 42 43 44 55 50 51 52 53 54 55 Let TRANSR = 'N'. RFP holds AP as follows: For UPLO = 'U' the upper trapezoid A(0:5,0:2) consists of the last three columns of AP upper. The lower triangle A(4:6,0:2) consists of the transpose of the first three columns of AP upper. For UPLO = 'L' the lower trapezoid A(1:6,0:2) consists of the first three columns of AP lower. The upper triangle A(0:2,0:2) consists of the transpose of the last three columns of AP lower. This covers the case N even and TRANSR = 'N'. RFP A RFP A 03 04 05 33 43 53 13 14 15 00 44 54 23 24 25 10 11 55 33 34 35 20 21 22 00 44 45 30 31 32 01 11 55 40 41 42 02 12 22 50 51 52 Now let TRANSR = 'T'. RFP A in both UPLO cases is just the transpose of RFP A above. One therefore gets: RFP A RFP A 03 13 23 33 00 01 02 33 00 10 20 30 40 50 04 14 24 34 44 11 12 43 44 11 21 31 41 51 05 15 25 35 45 55 22 53 54 55 22 32 42 52 We then consider Rectangular Full Packed (RFP) Format when N is odd. We give an example where N = 5. AP is Upper AP is Lower 00 01 02 03 04 00 11 12 13 14 10 11 22 23 24 20 21 22 33 34 30 31 32 33 44 40 41 42 43 44 Let TRANSR = 'N'. RFP holds AP as follows: For UPLO = 'U' the upper trapezoid A(0:4,0:2) consists of the last three columns of AP upper. The lower triangle A(3:4,0:1) consists of the transpose of the first two columns of AP upper. For UPLO = 'L' the lower trapezoid A(0:4,0:2) consists of the first three columns of AP lower. The upper triangle A(0:1,1:2) consists of the transpose of the last two columns of AP lower. This covers the case N odd and TRANSR = 'N'. RFP A RFP A 02 03 04 00 33 43 12 13 14 10 11 44 22 23 24 20 21 22 00 33 34 30 31 32 01 11 44 40 41 42 Now let TRANSR = 'T'. RFP A in both UPLO cases is just the transpose of RFP A above. One therefore gets: RFP A RFP A 02 12 22 00 01 00 10 20 30 40 50 03 13 23 33 11 33 11 21 31 41 51 04 14 24 34 44 43 44 22 32 42 52 Definition at line 188 of file stfttp.f. Author Generated automatically by Doxygen for LAPACK from the source code. Version 3.4.1 Sun May 26 2013 stfttp.f(3)
All times are GMT -4. The time now is 08:12 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy