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


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 !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-30-2012
Registered User
 
Join Date: May 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Grouping or appending the lines in a file through Unix

Hi,

I am looking for a way to Group the Lines in a file.. Basically My file structure is something like this


Code:
A 1 100 abc def 
A 1 200 abc def 
A 1 300 abc def 
A 2 100 pqr def 
A 2 200 pqr def 
A 2 300 pqr def 
A 1 100 abc def 
A 1 200 xyz def 
A 1 300 xyz def

I need it as shown below, Its Just to Group All records with 1 in the 2nd field


Code:
A 1 100 abc def 
A 1 200 abc def 
A 1 300 abc def 
A 1 100 abc def 
A 1 200 xyz def 
A 1 300 xyz def 
A 2 100 pqr def 
A 2 200 pqr def 
A 2 300 pqr def

It would be really great if you can tell me a logic to regroup and remove certain unwanted or duplicate record in the 2nd occurence.. If you see above you can find that "A 1 100 abc def " is repeated twice... I would neeed an output like this


Code:
A 1 100 abc def 
A 1 200 abc def 
A 1 300 abc def 
A 1 200 xyz def 
A 1 300 xyz def 
A 2 100 pqr def 
A 2 200 pqr def 
A 2 300 pqr def

Let me know if someone has idea on these lines

Last edited by joeyg; 05-30-2012 at 01:32 PM.. Reason: Please wrap data and scripts/commands with CodeTags.
Sponsored Links
    #2  
Old 05-30-2012
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Location: Out running a Marathon.
Posts: 2,191
Thanks: 48
Thanked 127 Times in 119 Posts
Seems a sort command would get you started.
Are you truly trying to sort on columns 2, then 4, and then 3?
Sponsored Links
    #3  
Old 05-30-2012
Registered User
 
Join Date: Jul 2008
Location: NY
Posts: 203
Thanks: 13
Thanked 39 Times in 38 Posts
you could use for your sample data
Code:
sort -k1,1 -k2,2 -k4,4 infile

but ideally you should use
Code:
sort -k1,1 -k4,4 -k2,2 infile

    #4  
Old 05-30-2012
Registered User
 
Join Date: May 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Actually joeyg, I just gave a sample.. My file is something similar in these lines but is very big with lot of lines and each line having lot of info... But when i used sort command, The output was getting jumbled...


Code:
A 1 100 abc def 
A 1 100 abc def 
A 1 200 abc def 
A 1 200 xyz def 
A 1 300 abc def 
A 1 300 xyz def 
A 2 100 pqr def 
A 2 200 pqr def 
A 2 300 pqr def

But this is not what i am expecting

Last edited by Scrutinizer; 05-30-2012 at 04:32 PM.. Reason: code tags
Sponsored Links
    #5  
Old 05-30-2012
Registered User
 
Join Date: Jul 2008
Location: NY
Posts: 203
Thanks: 13
Thanked 39 Times in 38 Posts
post what command you are using. Did you tried what is suggested in post #3
Sponsored Links
    #6  
Old 05-31-2012
jayan_jay's Avatar
Forum Advisor
 
Join Date: Jul 2008
Posts: 831
Thanks: 9
Thanked 185 Times in 176 Posts
expecting this ..

Code:
$ nawk '!x[$2$3]++' infile

Sponsored Links
    #7  
Old 05-31-2012
Banned
 
Join Date: Dec 2011
Posts: 193
Thanks: 22
Thanked 2 Times in 2 Posts
Use something like

Code:
sort -k1,1 -k2,2 -k4,4 tt | uniq

Sponsored Links
Closed Thread

Tags
append, group, reorder, sort, uniq

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
Grouping or appending the lines in a file through Unix mkandula1983 Shell Programming and Scripting 1 05-30-2012 01:34 PM
appending lines to a file dynamically Ananthdoss Shell Programming and Scripting 2 12-09-2011 07:41 AM
File splitting and grouping using unix script nandhan11891 Shell Programming and Scripting 4 04-20-2010 09:49 PM
grouping records in a file in unix trichyselva Shell Programming and Scripting 8 09-11-2009 03:31 AM
need help appending lines/combining lines within a file... mr_manny Shell Programming and Scripting 2 01-06-2006 05:45 PM



All times are GMT -4. The time now is 08:20 AM.