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: 19 December 2014, 1:03 AM EST
Location: Foxborough, MA
Posts: 7,679
Thanks: 156
Thanked 591 Times in 555 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 04:08 PM..
Sponsored Links
    #3  
Old 12-05-2012
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 16 December 2014, 11:19 AM EST
Location: Houston, Texas, USA
Posts: 676
Thanks: 0
Thanked 203 Times in 195 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 04: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 04: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 12:32 PM
How to use uniq on a certain field? Bandit390 Shell Programming and Scripting 1 12-02-2008 05:40 PM
Uniq using only the first field Digby UNIX for Dummies Questions & Answers 8 01-16-2008 06:25 AM
sort and uniq in perl reggiej Shell Programming and Scripting 4 05-18-2006 11:46 PM
sort/uniq jimmyflip UNIX for Dummies Questions & Answers 3 10-17-2002 06:09 AM



All times are GMT -4. The time now is 06:46 AM.