Transpose rows to certain columns


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Transpose rows to certain columns
# 1  
Old 02-08-2019
Transpose rows to certain columns

Hello,

I have the following data and I want to use awk to transpose each value to a certain column , so in case the value is not available the column should be empty.

Example:

Code:
Box Name: BoxA
Weight: 1
Length :2
Depth :3
Color: red
Box Name: BoxB
Weight: 3
Length :4
Color: Yellow
Box Name: BoxC
Weight: 5
Length :6
Depth :7
Box Name: BoxD
Weight: 8
Length :9

Desired Ouput:

Code:
Box Name,Weight,Length,Depth,Color
BoxA,1,2,3,red
BoxB,3,4,,Yellow
BoxC,5,6,7,
BoxD,8,9,,

Thanks you,

Last edited by rahman.ahmed; 02-08-2019 at 02:56 PM..
# 2  
Old 02-08-2019
Where exactly are you stuck with your attempt(s)?
Have you considered searching these forums? This is a very common ask.
Have you considered looking at the bottom of this thread for a list of suggested "hints"?

Please show what you've attempted so far....

Last edited by vgersh99; 02-08-2019 at 12:47 PM..
This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 02-08-2019
Hi,

Actually I don't have much experience in using awk. I got as far as transposing the columns and stuck there.

I used this
Code:
cat boxes.txt
Box Name: BoxA
Weight: 1
Length :2
Depth :3
Color: red
Box Name: BoxB
Weight: 3
Length :4
Color: Yellow
Box Name: BoxC
Weight: 5
Length :6
Depth :7
Box Name: BoxD
Weight: 8
Length :9

Code:
cat boxes.txt | awk '/^Box/ && c{print c;c=""}{c=c $0" "}END{if(c) print c}'
Box Name: BoxA Weight: 1 Length :2 Depth :3 Color: red
Box Name: BoxB Weight: 3 Length :4 Color: Yellow
Box Name: BoxC Weight: 5 Length :6 Depth :7
Box Name: BoxD Weight: 8 Length :9

I read the related threads in this forum but didn't find what I am looking for since the data is not identical for each row.

Thanks,
# 4  
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:
# 5  
Old 02-08-2019
Adapted this proposal published umpteen times in these fora to the requestor's sample



Code:
awk -F":"       '
BEGIN           {print HD = "Box Name,Weight,Length,Depth,Color"
                 for (MX=n=split (HD, HDArr, ","); n; n--) SRCH[HDArr[n]]
                }
NR > 1 &&
$0 ~ HDArr[1]   {for (i=1; i<=MX; i++) printf "%s%s", RES[HDArr[i]], (i == MX)?ORS:OFS
                 delete RES
                }

                {gsub (/ *: */, ":")
                }

$1 in SRCH      {RES[$1] = $2
                }

END             {for (i=1; i<=MX; i++) printf "%s%s", RES[HDArr[i]], (i == MX)?ORS:OFS
                }

' OFS="," file
Box Name,Weight,Length,Depth,Color
BoxA,1,2,3,red
BoxB,3,4,,Yellow
BoxC,5,6,7,
BoxD,8,9,,

These 3 Users Gave Thanks to RudiC For This Post:
# 6  
Old 02-08-2019
Don Cragun and RudiC ,

Thank you very much for your fast responses and help. I really appreciate it .

The proposed solution worked perfectly , The bash version I'm running is GNU bash, version 4.2.47(1)-release (x86_64-suse-linux-gnu)

Thanks again.
This User Gave Thanks to rahman.ahmed For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Login or Register to Ask a Question