How to swap the column to row?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to swap the column to row?
# 1  
Old 09-09-2014
How to swap the column to row?

Hi,
I am new to shell scripting and have one complex req like below. I have one file like below and want oupt put like the 2nd file.
Code:
Time|Measures|Product|store1|store2|store3|store4|store5|--
Week Ending 06-15-14|Dollar Sales|BACARDI PLUS|40|30|20|23|45|
Week Ending 06-22-14|Unit Sales|BACARDI PLUS|50|60|70|80|90|
Week Ending 06-21-14|volume Sales|BACARDI PLUS|33|44|55|66|77|

NB: Time column is having more other values,product can be more also .Measures are only these 3 values and stores are more store1,store2,store3....like this

O/P file
Code:
Time|product|storeDollar Sales|Unit Sales|volume Sales|
Week Ending 06-15-14|BACARDI PLUS|store1|40||
Week Ending 06-15-14|BACARDI PLUS|store2|30||
Week Ending 06-15-14|BACARDI PLUS|store3|20||
Week Ending 06-15-14|BACARDI PLUS|store4|23||
Week Ending 06-15-14|BACARDI PLUS|store5|45||
Week Ending 06-22-14|BACARDI PLUS|store1||50|
Week Ending 06-22-14|BACARDI PLUS|store2||60|
Week Ending 06-22-14|BACARDI PLUS|store3||70|
Week Ending 06-22-14|BACARDI PLUS|store4||80|
Week Ending 06-22-14|BACARDI PLUS|store5||90|
Week Ending 06-21-14|BACARDI PLUS|store1|||33
Week Ending 06-21-14|BACARDI PLUS|store2|||44
Week Ending 06-21-14|BACARDI PLUS|store3|||55
Week Ending 06-21-14|BACARDI PLUS|store4|||66
Week Ending 06-21-14|BACARDI PLUS|store4|||77

Can some body help me regarding this?


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data. Thanks

Last edited by jaggy; 09-09-2014 at 02:54 PM.. Reason: code tags
# 2  
Old 09-09-2014
Welcome jaggy,

I have a few to questions pose in response first:-
  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.


We're all here to learn and getting the relevant information will help us all.


Regards,
Robin
# 3  
Old 09-09-2014
Hi Robin,
Thanks for looking into it.

I am using Linux server to achieve this and trying with shell. I tried split function along with awk also. But I am not getting desired out put.

I tried to traverse each record with something like (i=1;i<=n;i++) {$2=s[i];print}} and when it will encounter the 3rd pipe it will sort the data afterwards in columnar fashion.

Something like below But not actual


Code:
awk '{n=split($3,s,"|");for (i=1;i<=n;i++) {$3=s[i];print}}' test.text


Last edited by rbatte1; 09-09-2014 at 01:22 PM.. Reason: Added ICODE tags
# 4  
Old 09-10-2014
Your spec is a bit inconsistent, a delimiter is missing between "store" and "Dollar" in the header; and also the delimiter count at line end differs between header and data lines. However, try
Code:
awk -F\| 'NR==1         {for (i=4; i<=NF; i++) ST[i]=$i
                         TTL="Time|product|store|Dollar Sales|Unit Sales|volume Sales|"
                         split (TTL, SEG)
                         print TTL
                         next
                        }
                        {for (j=4; j<7; j++) if ($2==SEG[j]) break;
                         for (i=4; i<=NF; i++)  {printf "%s|%s|%s|", $1, $3, ST[i]
                                                 for (k=4; k<j; k++) printf "|"
                                                 printf "%s", $i
                                                 for (k=j; k<7; k++) printf "|"
                                                 printf "\n"
                                                }
                        }
        ' file
Time|product|store|Dollar Sales|Unit Sales|volume Sales|
Week Ending 06-15-14|BACARDI PLUS|store1|40|||
Week Ending 06-15-14|BACARDI PLUS|store2|30|||
Week Ending 06-15-14|BACARDI PLUS|store3|20|||
Week Ending 06-15-14|BACARDI PLUS|store4|23|||
Week Ending 06-15-14|BACARDI PLUS|store5|45|||
Week Ending 06-15-14|BACARDI PLUS|--||||
Week Ending 06-22-14|BACARDI PLUS|store1||50||
Week Ending 06-22-14|BACARDI PLUS|store2||60||
Week Ending 06-22-14|BACARDI PLUS|store3||70||
Week Ending 06-22-14|BACARDI PLUS|store4||80||
Week Ending 06-22-14|BACARDI PLUS|store5||90||
Week Ending 06-22-14|BACARDI PLUS|--||||
Week Ending 06-21-14|BACARDI PLUS|store1|||33|
Week Ending 06-21-14|BACARDI PLUS|store2|||44|
Week Ending 06-21-14|BACARDI PLUS|store3|||55|
Week Ending 06-21-14|BACARDI PLUS|store4|||66|
Week Ending 06-21-14|BACARDI PLUS|store5|||77|
Week Ending 06-21-14|BACARDI PLUS|--||||

This User Gave Thanks to RudiC 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 from row to column using timestamp in first column

Gents, Transpose from row to column, taking in consideration the first column, which contends the date. Input file 72918,111000009,111000009,111000009,111000009,111000009,111000009,111000009,111000009,111000009 72918,2356,2357,2358,2359,2360,2361,2362,2363,2364 72918,0,0,0,0,0,0,0,0,0... (12 Replies)
Discussion started by: jiam912
12 Replies

2. Shell Programming and Scripting

Split column when value in column is blank in any row

Hi Experts, In short : Need to split file when field in column 5 is blank and need to generate two file in which column 5 is blank and other in which column 5 has values along with other rows and column data My issue is i am not able to get header for column from raw file into new file which... (1 Reply)
Discussion started by: as7951
1 Replies

3. Shell Programming and Scripting

Print first row of column a, last row of column b if column a has the same value

I have a table with this structure: cola colb colc 1 19 lemon 20 31 lemon 32 100 lemon 159 205 cherries 210 500 cherries and need to parse it into this format: cola colb colc 1 100 lemon 159 500 cherries So I need the first row of cola and the last row of colb if colc has the... (3 Replies)
Discussion started by: coppuca
3 Replies

4. Shell Programming and Scripting

Print row on 4th column to all row

Dear All, I have input : SEG901 5173 9005 5740 SEG902 5227 5284 SEG903 5284 5346 SEG904 5346 9010 SEG905 5400 5456 SEG906 5456 5511 SEG907 5511 9011 SEG908 5572 9015 SEG909 5622 9020 SEG910 5678 5739 SEG911 5739 5796 SEG912 5796 9025 ... (3 Replies)
Discussion started by: attila
3 Replies

5. Shell Programming and Scripting

Print every 5 4th column values as separate row with different first column

Hi, I have the following file, chr1 100 200 20 chr1 201 300 22 chr1 220 345 23 chr1 230 456 33.5 chr1 243 567 90 chr1 345 600 20 chr1 430 619 21.78 chr1 870 910 112.3 chr1 914 920 12 chr1 930 999 13 My output would be peak1 20 22 23 33.5 90 peak2 20 21.78 112.3 12 13 Here the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

6. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

7. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

8. Shell Programming and Scripting

Moving data from a specified column/row to another column/row

Hello, I have an input file like the following: 11_3_4 2_1_35 3_15__ _16989 Where '_' is a space. The data is in a table. Is there a way for the program to prompt the user for x1,y1 and x2,y2, where x1,y1 is the desired number (for example x=6 y=4 is a value of 4) and move to a desired spot... (2 Replies)
Discussion started by: jl487
2 Replies

9. Shell Programming and Scripting

row to column and position data in to fixed column width

Dear friends, Below is my program and current output. I wish to have 3 or 4 column output in order to accomodate in single page. i do have subsequent command to process after user enter the number. Program COUNT=1 for MYDIR in `ls /` do VOBS=${MYDIR} echo "${COUNT}. ${MYDIR}" ... (4 Replies)
Discussion started by: baluchen
4 Replies

10. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies
Login or Register to Ask a Question