LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-28-2012, 10:16 PM   #1
upendra_35
LQ Newbie
 
Registered: Oct 2012
Posts: 21

Rep: Reputation: Disabled
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

Last edited by colucix; 11-30-2012 at 10:51 AM. Reason: Changed PHP tags to CODE
 
Old 11-29-2012, 12:42 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
yes, sure, it is possible, you need to select the tools you want to use: awk/perl/sed.... What do you prefer?
 
Old 11-29-2012, 02:33 AM   #3
upendra_35
LQ Newbie
 
Registered: Oct 2012
Posts: 21

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
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.
 
Old 11-29-2012, 03:55 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
What about awk?
Code:
awk '{ _[$1] ? _[$1] = _[$1] ", " $2 : _[$1] = $2 } END{ for ( i in _ ) printf "%s\t%s\n", i, _[i] }' file
 
1 members found this post helpful.
Old 11-29-2012, 12:45 PM   #5
upendra_35
LQ Newbie
 
Registered: Oct 2012
Posts: 21

Original Poster
Rep: Reputation: Disabled
Thumbs up Thanks it worked

Quote:
Originally Posted by colucix View Post
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.

Last edited by colucix; 11-30-2012 at 10:52 AM. Reason: idem
 
Old 11-30-2012, 10:09 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 11-30-2012, 10:52 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by David the H. View Post
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].
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing table with Hpricot hattori.hanzo Programming 1 11-26-2010 06:59 AM
Code for At commands Parsing in C trkecea Linux - Networking 1 03-02-2010 07:38 AM
AT commands parsing code trkecea Linux - Software 3 02-03-2010 08:08 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration