LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   parsing table using linux commands (https://www.linuxquestions.org/questions/linux-newbie-8/parsing-table-using-linux-commands-4175439231/)

upendra_35 11-28-2012 10:16 PM

parsing table using linux commands
 
Hi i have a two column table as below

Code:

Bra033296        GO:0006355
Bra033296        GO:0003677
Bra033296        GO:0005634
Bra033295        GO:0005739
Bra033294        GO:0005634
Bra033294        GO:0006355
Bra033294        GO:0003677

Now i want to parse them as below

Code:

Bra033296      GO:0006355, GO:0003677, GO:0005634
Bra033295          GO:0005739
Bra033294      GO:0005634, GO:0006355, GO:0003677

Is it possible with linux commands?

Thanks

pan64 11-29-2012 12:42 AM

yes, sure, it is possible, you need to select the tools you want to use: awk/perl/sed.... What do you prefer?

upendra_35 11-29-2012 02:33 AM

Quote:

Originally Posted by pan64 (Post 4839273)
yes, sure, it is possible, you need to select the tools you want to use: awk/perl/sed.... What do you prefer?


Thanks....I don't have any preference.

colucix 11-29-2012 03:55 AM

What about awk?
Code:

awk '{ _[$1] ? _[$1] = _[$1] ", " $2 : _[$1] = $2 } END{ for ( i in _ ) printf "%s\t%s\n", i, _[i] }' file

upendra_35 11-29-2012 12:45 PM

Thanks it worked
 
Quote:

Originally Posted by colucix (Post 4839390)
What about awk?
Code:

awk '{ _[$1] ? _[$1] = _[$1] ", " $2 : _[$1] = $2 } END{ for ( i in _ ) printf "%s\t%s\n", i, _[i] }' file

This is exactly what i need. I just made a small edit to remove the space after ",".

Code:

awk '{ _[$1] ? _[$1] = _[$1] "," $2 : _[$1] = $2 } END{ for ( i in _ ) printf "%s\t%s\n", i, _[i] }'
Anyway thanks for your help. Much appreciated.

David the H. 11-30-2012 10:09 AM

Please don't use php code tags. There's a bug that keeps them from properly adjusting to the display width. Besides, this isn't php. Just use regular code tags.

Thanks.

colucix 11-30-2012 10:52 AM

Quote:

Originally Posted by David the H. (Post 4840245)
Please don't use php code tags. There's a bug that keeps them from properly adjusting to the display width. Besides, this isn't php. Just use regular code tags.

Thanks.

Indeed. Changed to [CODE][/CODE].


All times are GMT -5. The time now is 03:56 PM.