|
Search Forums:
|
|||||||
| Forums | Register | Forum Rules | Linux and Unix Links | Man Pages | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
Simple, sorted order: Code:
awk '
{ stuff[$1] = stuff[$1] $2 " " }
END {
for( s in stuff )
print s, stuff[s];
}' input-file |sortIf 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 | ||
|
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|