sort a file of 5 online by taking only the most recent

Hello, i develop a shell unix program and i have a csv file. every time we have 5 identical lines in the first 3 columns in the fourth column can be different and the 5th too. In the 5th I have a date in yyyy / mm / dd. I want to do a sort on the 5 lines and keep only the line where there is the most recent date. Example for this particular file

yyyy, bbbb, cccc; rgtff; 2009/04/03
yyyy, bbbb, cccc; rfggf; 2009/04/25
yyyy, bbbb, cccc; gnnhn; 2009/04/03
yyyy, bbbb, cccc; vvbvb; 2009/06/03
yyyy, bbbb, cccc; rbbbbvb; 2009/04/03
---
xxxx, yyyy, zzzz; rgtff; 2009/04/03
xxxx, yyyy, zzzz; rfggf; 2009/04/25
xxxx, yyyy, zzzz; gnnhn; 2009/04/03
xxxx, yyyy, zzzz; vvbvb; 2009/06/03
xxxx, yyyy, zzzz; rbbbbvb; 2009/04/03

In the end I must have

yyyy, bbbb, cccc; vvbvb; 2009/06/03
xxxx, yyyy, zzzz; vvbvb; 2009/06/03

Thank u very much in advance for your help

[editing the edited edit] needed -k 3.
There we go.

If not every line is data, and the first line isn't column names, then it's not a CSV file, just a file. :wink:

#!/bin/bash

while true
do
        for N in 0 1 2 3 4 5
        do
                read A || break
                echo "$A"
        done | sort -r -t ';' -k 3 | head -n 1
        read A || break
done < data

Unless I misunderstood, that's one line too many.

Regards,
Alister

1 Like

Excuse me but I did not understant very well the code can i explain me please. A variable that represents? thank u in advance

Good catch.

[quote="excuse me but i did not understant very well the code can i explain me please.[/quote"]
Sure.

what?

#!/bin/sh

# Loop until we tell it to stop
while true
do
        # Loop 5 times.  Each loop reads and writes one line.
        # We pipe it's output through sort, breaking fields on ; and 
        # sorting on the third field, and sort backwards.
        # Then we throw away everything but the first line.
        for N in 0 1 2 3 4
        do
                # Try to read a line.  If it fails, quit.
                read A || break
                # Print the line.
                echo "$A"
        done | sort -r -t ';' -k 3 | head -n 1
        # Read and ignore the lineful of dashes.  quit if that fails.
        read A || break
done < data
1 Like
sort -k1,3 -k5,5r in-file |sort -mus -k1,3
or
sort -t\; -k1,1 -k3,3r in-file |sort -t\; -mus -k1,1

Great its work thank u very much

How about this?

awk 'NF<5{close("sort -r -k5|head -1")}NF==5{print |"sort -r -k5|head -1"}' file
 yyyy, bbbb, cccc; vvbvb; 2009/06/03
 xxxx, yyyy, zzzz; vvbvb; 2009/06/03