LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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, 11: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 11:51 AM. Reason: Changed PHP tags to CODE
 
Old 11-29-2012, 01:42 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
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, 03: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, 04:55 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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, 01: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 11:52 AM. Reason: idem
 
Old 11-30-2012, 11:09 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

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

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 07:59 AM
Code for At commands Parsing in C trkecea Linux - Networking 1 03-02-2010 08:38 AM
AT commands parsing code trkecea Linux - Software 3 02-03-2010 09:08 AM


All times are GMT -5. The time now is 08:26 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration