The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to listout the files based on group by the date...? psiva_arul UNIX for Dummies Questions & Answers 3 04-21-2008 06:03 AM
Newly created files default group and write permissions goldfish UNIX for Dummies Questions & Answers 2 02-20-2008 02:39 PM
Recursive search for group or other writeable 'dot' files maficdan Security 5 02-14-2008 05:43 PM
Monkcast #12: IBM HW group OEMs Solaris to chagrin of SW group & a ... - ZDNet.com bl iBot UNIX and Linux RSS News 0 08-17-2007 01:30 PM
listing sequential files as one group... kentm UNIX for Advanced & Expert Users 1 01-24-2007 02:11 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-21-2005
Registered User
 

Join Date: Nov 2004
Posts: 20
group by in files :-)

My file looks like:

X^~^12^d
X^~^19^d
Y^~^5^d
Y^~^10^d
Y^~^11^d
Z^~^14^d

I need a script that will "group by" the first field and Concatenate the values.

My output should be:

X^~^12,19^d
Y^~^5,10,11^d
Z^~^14~d

Does anyone have any idea ?
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-21-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,636
Your post is on similiar lines to that of

shell script comparing files in a file

Check it out.


vino
Reply With Quote
  #3 (permalink)  
Old 06-21-2005
Registered User
 

Join Date: Nov 2004
Posts: 20
thank for the example but it doesn't handle

where the delimiter is ^ and if the concatenated field is not last.

Any idea ?
Reply With Quote
  #4 (permalink)  
Old 06-21-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,991
something to start with:

nawk -f hell.awk file.txt

hell.awk:
Code:
BEGIN { FS=OFS="^" }
{
    arr[$1] = ($1 in arr) ? arr[$1] "," $3 : $3
}
END {
  for (i in arr)
    print i OFS "~", arr[i]
}
Reply With Quote
  #5 (permalink)  
Old 06-21-2005
Registered User
 

Join Date: Nov 2004
Posts: 20
Thanks!!! Can I Marry You ? :-)

you are the best
Reply With Quote
  #6 (permalink)  
Old 06-21-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,991
Quote:
Originally Posted by hellsd
you are the best
take a number....
glad to be of help.
Reply With Quote
  #7 (permalink)  
Old 06-21-2005
Registered User
 

Join Date: Nov 2004
Posts: 20
Now it is more complicate:

Lets say the input file is:
ABC^1^~^data^x
ABC^2^~^data^x
ABC^3^~^data^x
DEF^4^~^data^y
DEF^4^~^data^z
DEF^5^~^data^y
DEF^5^~^data^z
DEF^6^~^data^z
DEF^6^~^data^y
XYZ^7^~^data^t

Now I would like to group couple of fields(field 2 and field 5)
My output should be:

ABC^1,2,3^~^data^x
DEF^4,5,6^~^data^y,z
XYZ^7^~^data^t

Notice that I would like to print all the line's fields.

Is it possible?
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:10 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0