sort and semi-duplicate row - keep latest only


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sort and semi-duplicate row - keep latest only
# 1  
Old 01-08-2009
sort and semi-duplicate row - keep latest only

I have a pipe delimited file. Key is field 2, date is field 5 (as example, my real file is more complicated of course, but the KEY and DATE are accurate)
There can be duplicate rows for a key with different dates.
I need to keep only rows with latest date in this case.
Example data:
W|AAA|DD|D|20080101
W|BBB|CC|C|20080101
W|AAA|BB|B|20080201
W|CCC|DD|D|20080701
W|CCC|EE|E|20080801
W|AAA|DD|D|20081231

I would want to see:
W|AAA|DD|D|20081231
W|BBB|CC|C|20080101
W|CCC|EE|E|20080801

I want to use sort for this but am open to other options. I'm guessing awk could be involved, but I'm bad at writing awk.
I've searched but didn't find anything that seemed to match.
Ideas?


edit to add a little more info:
It could be that I have rows with same key and same date. In that case, I'd prefer to take the one that is last in the file (because of differences in the other fields of the rows and how the file gets built) -- but if that is not possible I understand.

Last edited by LisaS; 01-08-2009 at 04:36 AM..
# 2  
Old 01-08-2009
I made something that is working on my small test file (the real file has too much data to really hand check) - I'd appreciate if someone could take a look and critique/agree/tell me I'm all wet.

sort -t"|" -k2,2 -kr5,5 <myrawfile.txt | sort -ru -k2,2 | sort -t"|" -k2,2

(I know the 3rd sort might be over the top, but it gets them back in key sequence)
# 3  
Old 01-08-2009
Code:
#! /usr/bin/perl
open FH,"a.txt";
while(<FH>){
	chomp;
	@arr=split("[|]",$_,3);
	$hash{$arr[1]}=$_;
}
close FH;
map {print $hash{$_},"\n"} sort keys %hash;

Code:
nawk '{
	split($0,arr,"|")
	_[arr[2]]=$0
}
END{
	for(i in _)
		print _[i]
}' a.txt | sort -t"|" +1

# 4  
Old 01-08-2009
Not sure if you are looking for this

Code:
$ awk '!x[$1,$2]++' FS="|" li.txt
W|AAA|DD|D|20080101
W|BBB|CC|C|20080101
W|CCC|DD|D|20080701

$ sort -t"|" -n -rk5 li.txt |  awk '!x[$1,$2]++' FS="|"
W|AAA|DD|D|20081231
W|CCC|EE|E|20080801
W|BBB|CC|C|20080101

# 5  
Old 01-08-2009
Something like this:

Code:
sort -t\| -k5nr infile|sort -t\| -uk2.1,2.3

With AWK (nawk or /usr/xpg4/bin/awk on Solaris):

Code:
awk -F\| 'END {
  for (k in r)
    print r[k]
	}
$NF > m[$2] {
  m[$2] = $NF
  r[$2] = $0 }
  ' file

Notice that the output will not be ordered by the second field.

Last edited by radoulov; 01-08-2009 at 05:48 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To Find Duplicate files using latest in Linux

I have tried the following code and with that i couldnt achieve what i want. #!/usr/bin/bash find ./ -type f \( -iname "*.xml" \) | sort -n > fileList sed -i '/\.\/fileList/d' fileList NAMEOFTHISFILE=$(echo $0|sed -e 's/\/()$*.^|/\\&/g') sed -i "/$NAMEOFTHISFILE/d"... (2 Replies)
Discussion started by: gold2k8
2 Replies

2. Shell Programming and Scripting

Delete duplicate row

Hi all, how can delete duplicate files in file form, e.g. $cat file1 aaa 123 234 345 456 bbb 345 345 657 568 ccc 345 768 897 456 aaa 123 234 345 456 ddd 786 784 234 263 ccc 345 768 897 456 aaa 123 234 345 456 ccc 345 768 897 456 then i need ouput file1 some, (4 Replies)
Discussion started by: aav1307
4 Replies

3. Shell Programming and Scripting

REMOVE DUPLICATE IN a ROW AFTER CHECKING THE FIRST SIMILAR NAME

Hi all I have a big file like this in rows and columns from 2 column onwards the next column is desciption of previous column means 3rd columns is description of 2 columns and 5 column is description of 4 column. All cloumns are separated by comma ... (1 Reply)
Discussion started by: manigrover
1 Replies

4. UNIX for Dummies Questions & Answers

Removing duplicate rows & selecting only latest date

Gurus, From a file I need to remove duplicate rows based on the first column data but also we need to consider a date column where we need to keep the latest date (13th column). Ex: Input File: Output File: I know how to take out the duplicates but I couldn't figure out... (5 Replies)
Discussion started by: shash
5 Replies

5. Shell Programming and Scripting

how to identify duplicate columns in a row

Hi, How to identify duplicate columns in a row? Input data: may have 30 columns 9211480750 LK 120070417 920091030 9211480893 AZ 120070607 9205323621 O7 120090914 120090914 1420090914 2020090914 2020090914 9211479568 AZ 120070327 320090730 9211479571 MM 120070326 9211480892 MM 120070324... (3 Replies)
Discussion started by: suresh3566
3 Replies

6. Shell Programming and Scripting

How to sort out the latest one from a series of numbers?

Hi, I have a directory which contains a number of sub directories. They are named as 1.0.0, 1.0.1, 1.0.2...1.1.0..1.1.1...1.2.0..and so on.. Basically these are the tags created at the time of release. Tags are named as major.minor.buildnumber format for modules. Now I have to search the... (2 Replies)
Discussion started by: bhaskar_m
2 Replies

7. Shell Programming and Scripting

Delete a row that has a duplicate column

I'm trying to remove lines of data that contain duplicate data in a specific column. For example. apple 12345 apple 54321 apple 14234 orange 55656 orange 88989 orange 99898 I only want to see apple 12345 orange 55656 How would i go about doing this? (5 Replies)
Discussion started by: spartan22
5 Replies

8. Shell Programming and Scripting

Deleting all occurences of a duplicate row

Hi, I need to delete all occurences of the repeated lines from a file and retain only the lines that is not repeated elsewhere in the file. As seen below the first two lines are same except that for the string "From BaseLine" and "From SMS".I shouldn't consider the string "From SMS" and "From... (7 Replies)
Discussion started by: ragavhere
7 Replies

9. Shell Programming and Scripting

delete semi-duplicate lines from file?

Ok here's what I'm trying to do. I need to get a listing of all the mountpoints on a system into a file, which is easy enough, just using something like "mount | awk '{print $1}'" However, on a couple of systems, they have some mount points looking like this: /stage /stand /usr /MFPIS... (2 Replies)
Discussion started by: paqman
2 Replies

10. Shell Programming and Scripting

Compare dates in a field and print the latest date row

Hi, I need a shell script which should find the latest date in the field of file and print that line only. For eg., I have a file /date.log Name Date Status IBM 06/06/07 close DELL 07/27/07 open DELL 06/07/07 open : : : From... (1 Reply)
Discussion started by: cvkishore
1 Replies
Login or Register to Ask a Question