Stacking Columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Stacking Columns
# 1  
Old 03-19-2013
Stacking Columns

How do I stack several columns of data into their own rows?
Code:
In:
A,B,C,d,e,f,g,H,I
1,2,3,4,5,6,7,8,9
 
Out:
A,B,C,X,Y,H,I
1,2,3,d,4,8,9
1,2,3,e,5,8,9
1,2,3,f,6,8,9
1,2,3,g,7,8,9

Mike

Last edited by Michael Stora; 03-20-2013 at 06:42 PM..
# 2  
Old 03-19-2013
If you expect anyone to devote any time to this, you should put some effort into explaining the logic behind the transformation, instead of leaving it for us to deduce. It may be obvious to you, but different people may draw different conclusions and squander their valuable time. And, please, do mention if there are any constraints or special cases, since they can drastically affect the complexity of the solution.

Is the magic secret that lowercase headers and their corresponding values should be converted to columns while the values under uppercase headers are simply extended to fill the matrix?

What is the point of this? Homework assignment?

Regards,
Alister
# 3  
Old 03-20-2013
Alister, stacking/unstacking is a standard term in data processing so I thought I would be understood. I guess it is more esoteric than I thought and I appologise. I usually do this in JSL or SAS but am looking for a shell script solution since a bunch of other processing is done in BASH. If there were any exceptions I would have mentioned them.

This is not homework as I am not a student. My actual data contains IP, so I use general/make-up data.

Example, Stacking the three columns in Red:
Code:
 
Name, Sex, Height, Weight, ID
Bob, M, 75, 220, 21345
Jill, F, 61, 120, 43563
Frank, M, 61, 300, 57864

Code:
 
Name, Label, Value, ID
Bob, Sex, M, 21345
Bob, Height, 75, 21345
Bob, Weight, 220, 21345
Jill, Sex, F, 43563
Jill, Height, 61, 43563
Jill, Weight, 120, 43563
Frank, Sex, M, 57864
Frank, Height, 61, 57864
Frank, Weight, 300, 57864

Mike
# 4  
Old 03-20-2013
You didn't answer the key question. When we look at the first line in your input, how do we know which fields are headers and which fields are an indication are data from following lines are to be turned into headers. In your first example, you list headers (e, f, g, and h) and we're magically supposed to know to throw away those data values in the following lines. In your second example, you have the headers Label and Value and we're magically supposed to know to get column headers our of the following data.

Please give us a clear, concise statement of the transformation that is supposed to occur to produce the output you need from the input you have.

The two examples you have provided are COMPLETELY different problems.
# 5  
Old 03-20-2013
I strongly agree with the others - YOU need to be more specific so that WE don't have to waste OUR time guessing and providing half-a** solutions that really don't fit.
Having said that.... here's my "guess" (based on your last example)
awk -f mic.awk myFile
mic.awk:
Code:
BEGIN { FS=OFS=","}
FNR==1 {
   n=split($0,hA,FS)
   print $1, "Label", "Value", $NF
   next
}
{
  for(i=2;i<NF;i++)
   print $1,hA[i],$i, $NF
}


Last edited by vgersh99; 03-20-2013 at 06:18 PM..
# 6  
Old 03-20-2013
Quote:
Originally Posted by Don Cragun
You didn't answer the key question. When we look at the first line in your input, how do we know which fields are headers and which fields are an indication are data from following lines are to be turned into headers. In your first example, you list headers (e, f, g, and h) and we're magically supposed to know to throw away those data values in the following lines. In your second example, you have the headers Label and Value and we're magically supposed to know to get column headers our of the following data.

Please give us a clear, concise statement of the transformation that is supposed to occur to produce the output you need from the input you have.

The two examples you have provided are COMPLETELY different problems.
I guess I could have been more clear in my first example, but the second example seems clear to me. The columns to stack are known ahead of time as are the stack parameter header label and value column label--no magic needed.

The desired transformation in the two examples is of the same type (note, I just fixed a small error in the first example, a missing value header).

Mike

Last edited by Michael Stora; 03-20-2013 at 06:43 PM..
# 7  
Old 03-20-2013
Quote:
Originally Posted by Michael Stora
I guess I could have been more clear in my first example, but the second example seems clear to me. The columns to stack are known ahead of time as are the stack parameter header label and value column label--no magic needed.

The desired transformation in the two examples is of the same type.

Mike
hm..... I really don't see how the first sample/result relates to the second one. I (for one) could not understand the first one at all - maybe others could have guessed better given your last explanation - I cannot....
So was my "guess" good-enough?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding columns from 2 files with variable number of columns

I have two files, file1 and file2 who have identical number of rows and columns. However, the script is supposed to be used for for different files and I cannot know the format in advance. Also, the number of columns changes within the file, some rows have more and some less columns (they are... (13 Replies)
Discussion started by: maya3
13 Replies

2. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns satisfy the condition

HI All, I'm embedding SQL query in Script which gives following output: Assignee Group Total ABC Group1 17 PQR Group2 5 PQR Group3 6 XYZ Group1 10 XYZ Group3 5 I have saved the above output in a file. How do i sum up the contents of this output so as to get following output: ... (4 Replies)
Discussion started by: Khushbu
4 Replies

3. Shell Programming and Scripting

Request: How to Parse dynamic SQL query to pad extra columns to match the fixed number of columns

Hello All, I have a requirement in which i will be given a sql query as input in a file with dynamic number of columns. For example some times i will get 5 columns, some times 8 columns etc up to 20 columns. So my requirement is to generate a output query which will have 20 columns all the... (7 Replies)
Discussion started by: vikas_trl
7 Replies

4. Shell Programming and Scripting

Compare 2 csv files by columns, then extract certain columns of matcing rows

Hi all, I'm pretty much a newbie to UNIX. I would appreciate any help with UNIX coding on comparing two large csv files (greater than 10 GB in size), and output a file with matching columns. I want to compare file1 and file2 by 'id' and 'chain' columns, then extract exact matching rows'... (5 Replies)
Discussion started by: bkane3
5 Replies

5. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns match on two rows

Hi all, I know this sounds suspiciously like a homework course; but, it is not. My goal is to take a file, and match my "ID" column to the "Date" column, if those conditions are true, add the total number of minutes worked and place it in this file, while not printing the original rows that I... (6 Replies)
Discussion started by: mtucker6784
6 Replies

6. Shell Programming and Scripting

Deleting all the fields(columns) from a .csv file if all rows in that columns are blanks

Hi Friends, I have come across some files where some of the columns don not have data. Key, Data1,Data2,Data3,Data4,Data5 A,5,6,,10,, A,3,4,,3,, B,1,,4,5,, B,2,,3,4,, If we see the above data on Data5 column do not have any row got filled. So remove only that column(Here Data5) and... (4 Replies)
Discussion started by: ks_reddy
4 Replies

7. Shell Programming and Scripting

Combine columns from many files but keep them aligned in columns-shorter left column issue

Hello everyone, I searched the forum looking for answers to this but I could not pinpoint exactly what I need as I keep having trouble. I have many files each having two columns and hundreds of rows. first column is a string (can have many words) and the second column is a number.The files are... (5 Replies)
Discussion started by: isildur1234
5 Replies

8. UNIX for Advanced & Expert Users

Help in Deleting columns and Renaming Mutliple columns in a .Csv File

Hi All, i have a .Csv file in the below format startTime, endTime, delta, gName, rName, rNumber, m2239max, m2239min, m2239avg, m100016509avg, m100019240max, metric3min, m100019240avg, propValues 11-Mar-2012 00:00:00, 11-Mar-2012 00:05:00, 300.0, vma3550a, a-1_CPU Index<1>, 200237463, 0.0,... (9 Replies)
Discussion started by: mahi_mayu069
9 Replies

9. Shell Programming and Scripting

Single command for add 2 columns and remove 2 columns in unix/performance tuning

Hi all, I have created a script which adding two columns and removing two columns for all files. Filename: Cust_information_1200_201010.txt Source Data: "1","Cust information","123","106001","street","1-203 high street" "1","Cust information","124","105001","street","1-203 high street" ... (0 Replies)
Discussion started by: onesuri
0 Replies

10. Shell Programming and Scripting

stacking case options

possible to stack case commands ? I get an error, s. comments below select choice in search delete quit do case $choice in search) select option in title director year do case $option in title) ..... break ;; ... (2 Replies)
Discussion started by: forever_49ers
2 Replies
Login or Register to Ask a Question