Sort field and uniq | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Sort field and uniq

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-05-2012
sabercats sabercats is offline
Registered User
 
Join Date: Feb 2006
Last Activity: 7 March 2014, 3:13 AM EST
Posts: 126
Thanks: 0
Thanked 0 Times in 0 Posts
Sort field and uniq

I have a flatfile A.txt


Code:
2012/12/04 14:06:07 |trees|Boards 2, 3|denver|mekong|mekong12
2012/12/04 17:07:22 |trees|Boards 2, 3|denver|mekong|mekong12
2012/12/04 17:13:27 |trees|Boards 2, 3|denver|mekong|mekong12
2012/12/04 14:07:39 |rain|Boards 1|tampa|merced|merced11

How do i sort and get latest date time with uniq duplicate field 6?
Out put will be

Code:
2012/12/04 17:13:27 |trees|Boards 2, 3|denver|mekong|mekong12
2012/12/04 14:07:39 |rain|Boards 1|tampa|merced|merced11

I used this code but did not work

Code:
sort -t\| +5 -6 0 -1 A.txt | uniq

Thanks for showing me.
Sponsored Links
    #2  
Old 12-05-2012
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 17 April 2014, 3:44 PM EDT
Location: Foxborough, MA
Posts: 7,523
Thanks: 137
Thanked 534 Times in 502 Posts
awk -f saber.awk myFile
where saber.awk is:

Code:
BEGIN {
  FS=OFS="|"
}
{
  f1=$1; gsub("[^0-9]","",f1)
  if (f1 > val[$NF]) {
    val[$NF]=f1
    arr[$NF]=$0
 }
}
END {
  for (i in arr)
    print arr[i]
}


Last edited by vgersh99; 12-05-2012 at 03:08 PM..
Sponsored Links
    #3  
Old 12-05-2012
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 17 April 2014, 5:23 PM EDT
Location: Houston, Texas, USA
Posts: 660
Thanks: 0
Thanked 200 Times in 192 Posts
try also (a little cheesy):

Code:
sort -r -t\| -k6 A.txt | awk -F"|" '!a[$6]++' | sort -t\| -k6

or

Code:
awk -F"|" '$1>a[$6]{a[$6]=$0}END {for (i in a) print a[i]}' A.txt | sort -t\| -k6


Last edited by rdrtx1; 12-05-2012 at 03:20 PM..
    #4  
Old 12-05-2012
sabercats sabercats is offline
Registered User
 
Join Date: Feb 2006
Last Activity: 7 March 2014, 3:13 AM EST
Posts: 126
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks all. It also work with

Code:
sort -t\| -k6,6 -u A.txt


Last edited by Franklin52; 12-06-2012 at 03:58 AM.. Reason: Please use code tags for data and code samples
Sponsored Links
Closed Thread

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
Help with Uniq and sort pinnacle Shell Programming and Scripting 4 05-06-2009 11:32 AM
How to use uniq on a certain field? Bandit390 Shell Programming and Scripting 1 12-02-2008 04:40 PM
Uniq using only the first field Digby UNIX for Dummies Questions & Answers 8 01-16-2008 05:25 AM
sort and uniq in perl reggiej Shell Programming and Scripting 4 05-18-2006 10:46 PM
sort/uniq jimmyflip UNIX for Dummies Questions & Answers 3 10-17-2002 05:09 AM



All times are GMT -4. The time now is 05:23 PM.