Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



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

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-08-2012
Registered User
 

Join Date: Oct 2009
Posts: 137
Thanks: 16
Thanked 0 Times in 0 Posts
grouping based on first column

I do have a tab delimited file of the following format

Code:

a_1 rt
a_1 st_2
a_1 st_3
a_2 bt_2
a_2 st_er
b_2 st_2
b_2 st_32
S_1 rt_8
S_1 rt_64

I want to cut short the above file and group the file based on the first column like below.


Code:
a_1 rt st_2  st_3
a_2 bt_2 st_er
b_2 st_2 st_32
S_1 rt_8 rt_64

My file is a big text file and I would like to know the best way to do it using awk or sed.
Please let me know
Sponsored Links
    #2  
Old 02-08-2012
agama agama is offline Forum Advisor  
Always Learning
 

Join Date: Jul 2010
Location: earth>US>UTC-5
Posts: 1,210
Thanks: 86
Thanked 405 Times in 389 Posts
Simple, sorted order:


Code:
awk '
    { stuff[$1] = stuff[$1] $2 " " }
    END {
        for( s in stuff )
            print s, stuff[s];
    }' input-file |sort

If order of output must match order that things in column 1 were seen:

Code:
awk '
    {
        if( !seen[$1]++ )
            order[++oidx] = $1;
        stuff[$1] = stuff[$1] $2 " "
    }
    END {
        for( i = 1; i <= oidx; i++ )
            print order[i], stuff[order[i]]
    }
' input-file

Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Please help me to find out maximum value of a field based on grouping of other fields. KamalKumarKalra UNIX for Dummies Questions & Answers 1 01-20-2012 09:45 PM
Help with grouping data based on range position perl_beginner Shell Programming and Scripting 1 09-09-2011 02:17 PM
need to remove duplicates based on key in first column and pattern in last column script_op2a Shell Programming and Scripting 13 05-18-2011 06:21 PM
Compare files column to column based on keys blackjack101 Shell Programming and Scripting 2 03-31-2010 06:46 PM
Change names in a column based on the symbols in another column repinementer Shell Programming and Scripting 8 08-14-2009 03:30 AM



All times are GMT -4. The time now is 04:48 AM.