Pivoting values from column to rows

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Pivoting values from column to rows
# 8  
Old 04-15-2017
Output from "my" proposal:
Code:
2017/02/01        10        20        30        70
2017/01/01        10        20        40

So - please explain in detail WHAT goes wrong in WHICH way. "then it fails" doesn't really help.
# 9  
Old 04-15-2017
Quote:
Originally Posted by Don Cragun
In what way did my suggestion fail? If I change the last line of your sample file as suggested in post #6, I get the following output from the code I suggested in post #4:
Code:
		a	b	c	d
2017/01/01	10	20	40	
2017/02/01	10	20		30

What output were you expecting?

PS: Note that the code I suggested prints out columns in the order in which input field 2 values were first seen and prints out rows in the order in which input field 1 values were first seen.

The simpler and faster code RudiC suggested prints rows and columns in random order (but the values in each column should be consistent).
Booo,
I apologize... The output above was produced by changing the 6th row in your sample input file to the line you provided in post #6, instead of by adding that line as a new 7th row. When I add that new line to your original sample data, the output produced by my suggestion is:
Code:
		a	b	c	d
2017/01/01	10	20	40	
2017/02/01	10	20	30	30

with my suggested code, with that input file, the output will always be in this order.

When I feed that input to the code RudiC provided, I get the output:
Code:
                   d         a         b         c
2017/01/01                  10        20        40
2017/02/01        30        10        20        30

but the order of the last four columns and the order of the rows may vary with different versions of awk (because the order in which items in an array are processed by for(index in array_name) is not specified by the standards). Note that the output RudiC got using the version of awk on his system produced output with the order of the last four columns AND the order of the rows printed was different than what I got on my system, but the numeric values in the table of output values displayed was identical using both my suggestion and his suggestion (on both of our systems). Although it may be hard to tell looking at the output he provided in post #8 since he didn't include the header line in the output.

As RudiC said, just saying that "it fails" without specifying whose suggested code you're running and without specifying what is wrong with the output produced is not at all helpful.
# 10  
Old 04-15-2017
I apologize - just didn't capture the entire output, which reads
Code:
                   a         b         c         d
2017/02/01        10        20        30        70
2017/01/01        10        20        40

# 11  
Old 04-15-2017
Can we replace the empty spaces in the column to zero. If its empty while sending as an excel format the empty column is copied by adjacent column data.
# 12  
Old 04-15-2017
Quote:
Originally Posted by Booo
Can we replace the empty spaces in the column to zero. If its empty while sending as an excel format the empty column is copied by adjacent column data.
Yes. You can do that by changing 1 character in my suggestion or by adding 2 characters (4 if you add spaces around arithmetic operators) to either of our suggestions.

Can we know which of our suggestions you're using?

Can we know what fails in the suggestions that have been provided for you? (In other words, please explain in what way the suggestions you were given did not do what you requested in the requirements you stated before adding your new requirement in post #11?)

Can you try to modify the code we have supplied instead of expecting us to change our code for you every time you change your requirements? If you can't get it to work, show us what you have a tried an we'll be glad to help you finish it.
This User Gave Thanks to Don Cragun For This Post:
# 13  
Old 04-15-2017
Thanks Don. I tried to change the OFS value but it didn't work. I am not sure how to fix this one.
# 14  
Old 04-15-2017
Quote:
Originally Posted by Booo
Thanks Don. I tried to change the OFS value but it didn't work. I am not sure how to fix this one.
I asked three questions in post #12 in this thread. You answered one of them (although I have no idea why you thought changing the output field separator would change the value of a field from an empty string into a numeric string with value 0). We know that you understand how both of our suggestions work (since you're asking questions here to learn how to solve your problem and you haven't asked any questions about how the suggested awk scripts work). At least we hope that you understand that this forum is here to help you learn how to use BSD, Linux, and UNIX system shell scripting to solve your own problems; not to act as your unpaid programming staff. Smilie

If you're unwilling to tell us how our suggestions failed to meet your stated requirements and you aren't even willing to tell us which of our suggestions you're trying to get to work, why should we waste any more time trying to help you? Smilie

Please help us help you by answering the questions we have asked!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare values in multiple rows in one column using awk

I would like to compare values in column 8, and grep the ones where the different is > 1, columns 1 and 2 are the key for array. Every 4 rows the records values in columns 1 and 2 changed. Then, the comparison in the column 8 need to be done for the 4 rows everytime columns 1 and 2 changed ... (4 Replies)
Discussion started by: jiam912
4 Replies

2. Shell Programming and Scripting

Convert Column data values to rows

Hi all , I have a file with the below content Header Section employee|employee name||Job description|Job code|Unitcode|Account|geography|C1|C2|C3|C4|C5|C6|C7|C8|C9|Csource|Oct|Nov|Dec|Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep Data section ... (1 Reply)
Discussion started by: Hypesslearner
1 Replies

3. Shell Programming and Scripting

Choosing rows based on column values

I have a .csv file: A,B,0.6 C,D,-0.7 D,E,0.1 A,E,0.45 D,G, -0.4 I want to select rows based on the values of the 3rd columns such that it is >=0.5 or <= -0.5 Thanks. A,B,0.6 D,G, -0.7 (1 Reply)
Discussion started by: Sanchari
1 Replies

4. Shell Programming and Scripting

Remove rows with e column values

Hi All, I have a big file with 232 columns and 9 million rows, I want to delete all rows with same column values in col3 through col232. Also the output should be sorted based on first 2 columns. Here is a reduced example with 6 columns. I want to remove rows with duplicate values in col3... (9 Replies)
Discussion started by: alpesh
9 Replies

5. Shell Programming and Scripting

Transposing rows and columns (pivoting) using shell scripting

Here is the contents of an input file. A,1,2,3,4 10,aaa,bbb,ccc,ddd 11,eee,fff,ggg,hhh 12,iii,jjj,lll,mmm 13,nnn,ooo,ppp I wanted the output to be A 10 1 aaa 10 2 bbb 10 3 ccc 10 4 ddd 11 1 eee 11 2 fff 11 3 ggg 11 4 hhh ..... and so on How to do it in ksh... (9 Replies)
Discussion started by: ksatish89
9 Replies

6. Shell Programming and Scripting

join rows based on the column values

Hi, Please help me to convert the input file to a new one. input file: -------- 1231231231 3 A 4561223343 0 D 1231231231 1 A 1231231231 2 A 1231231231 4 D 7654343444 2 A 4561223343 1 D 4561223343 2 D the output should be: -------------------- 1231231231 3#1#2 A 4561223343 0 D... (3 Replies)
Discussion started by: vsachan
3 Replies

7. UNIX for Dummies Questions & Answers

count number of rows based on other column values

Could anybody help with this? I have input below ..... david,39 david,39 emelie,40 clarissa,22 bob,42 bob,42 tim,32 bob,39 david,38 emelie,47 what i want to do is count how many names there are with different ages, so output would be like this .... david,2 emelie,2 clarissa,1... (3 Replies)
Discussion started by: itsme999
3 Replies

8. UNIX for Dummies Questions & Answers

How to assign scores to rows based on column values

Hi, I'm trying to assign a score to each row which will allow me to identify which rows differ. In the example file below, I've used "," to indicate column separators (my actual file has tab separators). In this example, I'd like to identify that row 1 and row 5 are the same, and row 2 and row... (4 Replies)
Discussion started by: auburn
4 Replies

9. Shell Programming and Scripting

How to consolidate values in one column from different rows into one?

Hi Guys, Thank you all for helping me with my different queries and I continue to get better at scripting because of help from all of you! I have a file that would look something like - ID SUB ID VALUE 1 10 5 2 18 7 1 ... (1 Reply)
Discussion started by: sncoupons
1 Replies

10. Shell Programming and Scripting

Pivoting Dynamic rows into columns

Original file we are getting ....... Item Period Amt P1 106 1000 P1 206 1500 P1 106 2000 P2 256 5800 P2 650 7500 My output should be like this Item 106 206 256 650 ............ P1 1000 1500 0 ... (1 Reply)
Discussion started by: dprakash
1 Replies
Login or Register to Ask a Question