The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
merging similar lines adambot Shell Programming and Scripting 2 05-05-2009 03:52 PM
Urgent help needed on merging lines with similar words awb221 Shell Programming and Scripting 3 02-23-2009 11:16 PM
Merging two command into single line kingganesh04 UNIX for Advanced & Expert Users 2 02-04-2009 09:45 AM
Merging columns from multiple files in one file isgoed Shell Programming and Scripting 1 08-03-2008 09:54 PM
merging few columns of two text files to a new file kolvi Shell Programming and Scripting 4 09-15-2005 04:34 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-27-2009
repinementer repinementer is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 150
merging similar columns in a single line file

Hi Guys. I have tried the commands sort and join. But I couldn't able to to find the command for joining in a single line based on keys.My example inputs and outputs are like the following.
Help would be appreciated.


Input file

a1tabXXXXXXX
a2tabXXXXXXX
a6tabYYYYYYYYY
a71tabXXXXXXX
a62tabYYYYYYYYY
a30tabZZZZZ
a13tabZZZZZ
a43tabZZZZZ
a23tabZZZZZ

outputfile

XXXXXXXtaba1taba2taba71
YYYYYYYYYtaba6taba62tab
ZZZZZtaba30taba13taba43tab23
Bits Awarded / Charged to repinementer for this Post
Date User Comment Amount
05-28-2009 nshailesh N/A 24
  #2 (permalink)  
Old 05-27-2009
devtakh devtakh is offline
Registered User
  
 

Join Date: Oct 2007
Location: Bangalore
Posts: 514
Code:
awk '{k=$2;a[k]=a[k]"\t"$1}END{for(k in a)print k,a[k]}' OFS="\t" filename

-Devaraj Takhellambam
  #3 (permalink)  
Old 05-27-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,422
you should put in some effort next time by showing what you have done

If you have Python
Code:
#!/usr/bin/env python
d={}
for line in open("file"):
    line=line.strip().split()
    d.setdefault(line[-1],[])
    d[line[-1]].append(line[0])
for i,j in d.iteritems():
    print i, '\t'.join(j)
output
Code:
YYYYYYYYY       a6      a62
ZZZZZ   a30     a13     a43     a23
XXXXXXX         a1      a2      a71
  #4 (permalink)  
Old 05-27-2009
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,382
Quote:
Originally Posted by devtakh View Post
Code:
awk '{k=$2;a[k]=a[k]"\t"$1}END{for(k in a)print k,a[k]}' OFS="\t" filename

-Devaraj Takhellambam
great solution but that will place two tab after first column so making small change
Code:
 
awk '{k=$2;a[k]=a[k]"\t"$1}END{for(k in a)print k,a[k]}' filename
  #5 (permalink)  
Old 05-27-2009
repinementer repinementer is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 150
Yep

Thanx Guys the both scripts working great.
Well ghost Dog I tried to write script in perl but the sript was giving funny values and wasted 3 hrs. I didn't want to put the scipt coz that sript doesn't make any sense.
But thanx for the advice
  #6 (permalink)  
Old 05-28-2009
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2009
Posts: 489
Quote:
Originally Posted by repinementer View Post
... I tried to write script in perl but the sript was giving funny values and wasted 3 hrs. I didn't want to put the scipt coz that sript doesn't make any sense.
...
Ah, the glorious messiness of Perl:

Code:
$
$ perl -ne 'split; $x{$_[1]}=$x{$_[1]}."\t".$_[0]; END{foreach $k(keys %x){print $k,"\t",$x{$k},"\n"}}' input.txt
XXXXXXX         a1      a2      a71
ZZZZZ           a30     a13     a43     a23
YYYYYYYYY               a6      a62
$
$
As Larry Wall says - "Easy things should be easy, and hard things should be possible."
Amazon.com Message

Cheers,
tyler_durden
Bits Awarded / Charged to durden_tyler for this Post
Date User Comment Amount
05-28-2009 vidyadhar85 Great quote!! 1,000
  #7 (permalink)  
Old 05-28-2009
repinementer repinementer is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 150
cool

Awesome Tyler

Last edited by repinementer; 05-28-2009 at 04:26 AM..
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 06:41 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0