Combine Similar Output from the 2nd field w.r.t 1st Field


 
Thread Tools Search this Thread
# 1  
Old 04-28-2016
Combine Similar Output from the 2nd field w.r.t 1st Field

Hi,

For example:

I have:

Code:
HostA,XYZ
HostB,XYZ
HostC,ABC

I would like the output to be:

Code:
HostA,HostB: XYZ
HostC:ABC

How can I achieve this?

So far what I though of is:
1. Look through 2nd field and
Code:
sort

and
Code:
uniq

it
2. Use what I have from 1. and
Code:
grep

the first field.
3. Get the first field I got from
Code:
grep

and group them by
Code:
comma

# 2  
Old 04-28-2016
I dont really understand your approach, but this
Code:
awk -F, '{T[$2] = T[$2] FS $1} END {for (t in T) print substr (T[t],2), t}' OFS=: file
HostA,HostB:XYZ
HostC:ABC

will turn your input sample into the desired output (uncertain if you want spaces in it or not).
This User Gave Thanks to RudiC For This Post:
alvinoo (04-29-2016)
 

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
awk to combine all matching fields in input but only print line with largest value in specific field cmccabe Shell Programming and Scripting 0 12-29-2016 02:40 PM
awk to combine matches and use a field to adjust coordinates in other fields cmccabe Shell Programming and Scripting 4 07-21-2016 10:37 AM
awk to combine by field and average by another cmccabe Shell Programming and Scripting 3 02-18-2016 03:58 PM
Combine identical lines and average the one variable field jfern Shell Programming and Scripting 2 06-11-2014 08:05 AM
Combine table field for time stew UNIX for Dummies Questions & Answers 4 02-24-2014 01:55 PM
sed to work on 2nd field only krishmaths Shell Programming and Scripting 2 04-01-2013 07:19 AM
Plz Help. Compare 2 files field by field and get the output in another file. i150371485 Shell Programming and Scripting 5 07-26-2012 12:32 AM
Compare two files Field by field and output the result in another file i150371485 Shell Programming and Scripting 7 07-20-2012 07:01 AM
Combine multiple lines in file based on specific field ratheesh2011 Shell Programming and Scripting 8 07-11-2012 02:39 PM
how to find the 2nd field pareshpatra Shell Programming and Scripting 1 08-18-2011 10:24 AM
Append 1st field from a file into 2nd field of another file amurib Shell Programming and Scripting 1 12-25-2010 05:13 AM
Appending 1st field in a file into 2nd field in another file amurib Shell Programming and Scripting 0 12-25-2010 04:49 AM
Sort alpha on 1st field, numerical on 2nd field (sci notation) FrancoisCN Shell Programming and Scripting 1 06-12-2009 10:45 AM
get a name according to the 2nd field melanie_pfefer Shell Programming and Scripting 4 01-28-2009 11:47 AM
replace a similar field in a file chiru_h Shell Programming and Scripting 2 11-01-2006 04:23 PM