Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Sort field and uniq

Shell Programming and Scripting


Closed    
 
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 offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 8 December 2016, 5:30 AM EST
Location: Foxborough, MA
Posts: 7,989
Thanks: 195
Thanked 699 Times in 655 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 Unix and Linux 12-05-2012
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 7 December 2016, 9:36 PM EST
Location: Houston, Texas, USA
Posts: 873
Thanks: 0
Thanked 299 Times in 286 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 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 04:58 AM.. Reason: Please use code tags for data and code samples
Sponsored Links
Closed

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 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 07:49 AM.