Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Sort field and uniq

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-05-2012
sabercats sabercats is offline
Registered User
 
Join Date: Feb 2006
Last Activity: 10 July 2015, 12:44 PM EDT
Posts: 130
Thanks: 1
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 Unix and Linux 12-05-2012
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is online now Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 28 August 2015, 6:09 PM EDT
Location: Foxborough, MA
Posts: 7,871
Thanks: 176
Thanked 667 Times in 624 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 Unix and Linux 12-05-2012
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 18 August 2015, 4:23 PM EDT
Location: Houston, Texas, USA
Posts: 685
Thanks: 0
Thanked 206 Times in 198 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 Unix and Linux 12-05-2012
sabercats sabercats is offline
Registered User
 
Join Date: Feb 2006
Last Activity: 10 July 2015, 12:44 PM EDT
Posts: 130
Thanks: 1
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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 06:17 PM.