Shell command to transpose Row & Column

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Shell command to transpose Row & Column
# 1  
Old 05-03-2018
Shell command to transpose Row & Column

Hi all,

need a help with getting a one line command to do the following.
i have an input file with rows of data containing credits for each office, the output should be one row for each office with all the credits in rows for that office, if its not there then it should say N/A. the credits are fixed according to the office to 4.
Appreciate your help.

Input:
Code:
Region	State	City	Office	credit
SE	Georgia	Atlanta	Admin	92
SE	Georgia	Atlanta	HR	45
SE	Georgia	Atlanta	Tech	150
SE	Georgia	Atlanta	Soft	250
MW	Texas	Dallas	Admin	400
MW	Texas	Dallas	Tech	121

Output:
Code:
Region	State	City	Admin	HR	Tech	Soft
SE	Georgia	Atlanta	92	45	150	250
MW	Texas	Dallas	400	N/A	121	N/A

# 2  
Old 05-03-2018
Assuming this is homework because of your written condition - "one line command". If this is a real problem, please explain and also include what you have tried.

Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.
# 3  
Old 05-03-2018
How far would this (admittedly clumsy) approach get you?
Code:
awk '
        {CAT[$1,$2,$3] = NR
         OFF[$4] = NR
         T[$1,$2,$3,$4] = $5
        }

END     {for (c in CAT) {printf "%s", c
                         for (o in OFF) if (OFF[o]!=1) printf "\t%s", CAT[c]==1?o:T[c,o]?T[c,o]:"N/A"
                         printf RS
                        }
        }
' SUBSEP="\t" file
Region	State	City	Soft	Admin	HR	Tech
MW	Texas	Dallas	N/A	400	N/A	121
SE	Georgia	Atlanta	250	92	45	150


Last edited by RudiC; 05-04-2018 at 04:17 PM..
# 4  
Old 05-04-2018
sorry for the late reply, this is definitely not homework.
i work on cisco platform, with some files having data close to what i asked for.
the reason i asked for a one liner is coz it needs to be incorporated in already existing script.
This is what i tried so far with no luck.

Code:
perl -ane 'BEGIN{$lc=0;} if ($lc==0) {@r=@F;$lc++;} else {for $i (1 .. $#r) {print "$F[0]\t$r[$i]\t$F[$i]\n";}$lc=0;@r=();}' sample.txt
SE      State   Georgia
SE      City    Atlanta
SE      Office  Admin
SE      credit  92
SE      Georgia Georgia
SE      Atlanta Atlanta
SE      HR      Tech
SE      45      150
MW      Georgia Texas
MW      Atlanta Dallas
MW      Soft    Admin
MW      250     400


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 05-04-2018 at 04:15 PM.. Reason: Added CODE tags.
# 5  
Old 05-04-2018
See post#3
This User Gave Thanks to RudiC For This Post:
# 6  
Old 05-04-2018
thank you rudic but iam getting the following error.

Code:
awk: {CAT[$1,$2,$3]=NR OFF[$4]=NR T[$1,$2,$3,$4]=$5} END {for (c in CAT) {printf "%s", c for (o in OFF) if (OFF[o]!=1) printf "\t%s", CAT[c]==1?o:T[c,o]?T[c,o]:"N/A" printf RS}}
awk:                          ^ syntax error
awk: {CAT[$1,$2,$3]=NR OFF[$4]=NR T[$1,$2,$3,$4]=$5} END {for (c in CAT) {printf "%s", c for (o in OFF) if (OFF[o]!=1) printf "\t%s", CAT[c]==1?o:T[c,o]?T[c,o]:"N/A" printf RS}}
awk:                                            ^ syntax error
awk: {CAT[$1,$2,$3]=NR OFF[$4]=NR T[$1,$2,$3,$4]=$5} END {for (c in CAT) {printf "%s", c for (o in OFF) if (OFF[o]!=1) printf "\t%s", CAT[c]==1?o:T[c,o]?T[c,o]:"N/A" printf RS}}
awk:                                                                                     ^ syntax error

# 7  
Old 05-04-2018
You should NOT modify the structure of a script / program without knowing EXACTLY what you're doing.
When shoving all of the lines of a structured / indented file into one single line, you AT LEAST need to insert semicolons instead of the line breaks.

Last edited by RudiC; 05-04-2018 at 05:04 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Transpose the Row and column

Hi, I have data in form of A ram B shyam C seeta D geeta A bob B methew C Richad D Mike and i want it in this form. A B C D ram shyam seeta geeta bob methew Richard Mike. please help by providing the scripting for this. (3 Replies)
Discussion started by: ricbha
3 Replies

2. Shell Programming and Scripting

How transpose column in a row?

Hello guys, First of all happy holidays and happy new year. I'm new in bioinformatic and also it is my first time that I write in this forum. Therefore, sorry if I make some mistakes. I'm writing to ask your help to fix a problem: I have a file like this: gene1 GO:0016491|GO:0055114... (8 Replies)
Discussion started by: Salvatore_espos
8 Replies

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

4. Shell Programming and Scripting

Transpose row to column

I'm using the testawk.awk from the following thread https://www.unix.com/shell-programming-and-scripting/18897-row-column-transpose.htmlI'm getting the following output fieldname1 data1 fieldname2 data2 fieldname3 data3 How can I get like this instead 1 fieldname1 data1 2 fieldname2 data2... (1 Reply)
Discussion started by: makkan
1 Replies

5. Shell Programming and Scripting

To transpose row into column

Hi All, In shell, I have below data coming from some some text file as below: . 351706 5861.8 0.026 0.012 12.584 0.026 0.012 12.582 0.000 0.000 0.000 Now i need the above data to be transposed as below 351706... (16 Replies)
Discussion started by: Anamica
16 Replies

6. Shell Programming and Scripting

Column to row Transpose

Hi Folks, Iam a kinda newbie to unix shell scripting, the scenario is i have a text file containing the following info Charlie chicago 15 Charlie newyork 26 jonny chicago 14 jonny newyork 15 joe chicago 15 joe newyork 18output should be Name chicago ... (3 Replies)
Discussion started by: tech_frk
3 Replies

7. UNIX for Dummies Questions & Answers

Row to column transpose

Hi there, Below is sample three rows which i need transpose into multiple rows. By keeping first 2 fields static and split them into multiple rows depend following date field. Each into seperate rows. Sample code: ... (6 Replies)
Discussion started by: ganeshd
6 Replies

8. Shell Programming and Scripting

Row to column transpose between same pattern.

Hi All, I have been trying to transpose rows to column in an large file (about 15000 lines) between matching pattern. Searched all posts in this forum, but not able find the solution to my problem. Any help appreciated.!! Input /*------XXXXXX-------*/ owner: XXXX location: XXXX... (3 Replies)
Discussion started by: RobP
3 Replies

9. Shell Programming and Scripting

Transpose column to row

Hi i have a file which has values seperated by "," as shown below and I want to transpose for every doc_id in one row. Input: DOC_ID,KEYWORD 105,REGISTROS 105,GEOLOGIA 105,NUCLEOS 105,EXPEDIENTE 105,PROGRAMAS 10025,EXPEDIENTE 10025,LOCALIZACIONES 10025,OFICIOS 10025,PROGRAMAS... (4 Replies)
Discussion started by: juelillo
4 Replies

10. Shell Programming and Scripting

Row to column transpose

Can we transpose rows to columns? Fields within row are separated by a comma. (10 Replies)
Discussion started by: videsh77
10 Replies
Login or Register to Ask a Question