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
SVN::Dump::Headers(3pm) 				User Contributed Perl Documentation				   SVN::Dump::Headers(3pm)

NAME
SVN::Dump::Headers - Headers of a SVN dump record SYNOPSIS
# SVN::Dump::Headers objects are returned by the read_header_block() # method of SVN::Dump::Reader DESCRIPTION
A "SVN::Dump::Headers" object represents the headers of a SVN dump record. METHODS
"SVN::Dump::Headers" provides the following methods: new( [$hashref] ) Create and return a new empty "SVN::Dump::Headers" object. If $hashref is given (it can be a blessed hash reference), the keys from the hash are used to initialise the headers. set($h, $v) Set the $h header to the value $v. "_" can be used as a replacement for "-" in the header name. get($h) Get the value of header $h. "_" can be used as a replacement for "-" in the header name. keys() Return the list of headers, in canonical order. as_string() Return a string that represents the record headers. type() It is possible to guess the record type from its headers. This method returns a string that represents the record type. The string is one of "revision", "node", "uuid" or "format". The method dies if it can't determine the record type. ENCAPSULATION
When using "SVN::Dump" to manipulate a SVN dump, one should not directly access the "SVN::Dump::Headers" component of a "SVN::Dump::Record", but use the "set_header()" and "get_header()" methods of the record object. SEE ALSO
"SVN::Dump::Record". COPYRIGHT
Copyright 2006-2011 Philippe 'BooK' Bruhat, All Rights Reserved. LICENSE
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.10.1 2011-03-22 SVN::Dump::Headers(3pm)
All times are GMT -4. The time now is 01:24 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy