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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sort a file of 5 online by taking only the most recent
# 1  
Old 03-04-2011
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
# 2  
Old 03-04-2011
[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. Smilie

Code:
#!/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


Last edited by Corona688; 03-04-2011 at 07:32 PM..
# 3  
Old 03-04-2011
Quote:
Originally Posted by Corona688
Code:
for N in 0 1 2 3 4 5

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

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 4  
Old 03-04-2011
Explain me please

Excuse me but I did not understant very well the code can i explain me please. A variable that represents? thank u in advance
# 5  
Old 03-04-2011
Quote:
Originally Posted by alister
Unless I misunderstood, that's one line too many.

Regards,
Alister
Good catch.

[quote=Excuse me but I did not understant very well the code can i explain me please.[/quote] Sure.
Quote:
A variable that represents?
what?

Code:
#!/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

This User Gave Thanks to Corona688 For This Post:
# 6  
Old 03-04-2011
Code:
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

# 7  
Old 03-05-2011
Quote:
Originally Posted by Corona688
Good catch.

Sure. what?

Code:
#!/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

Great its work thank u very much
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort Unique Column with the most recent timestamp

Hello, I have a sample file with the below contents : Backup Oracle8_P112_PEGA_Archivedel Completed full 10/11/2015 03:50:06PM Backup Oracle8_G567_PEGA_Archivedel Completed full 10/11/2015 01:15:56PM Backup Oracle8_P112_PEGA_Archivedel Completed full ... (8 Replies)
Discussion started by: rahul2662
8 Replies

2. Solaris

Recent file available

Dear, Require a script to check : If the file under /opt/OV/log directory has recent 5 hours(or user defined value) file generated or not If generated then cmd to call : opcmsg a=a o=o msg_grp=OpC msg_text="Log file didn't generated on $time" s=critical (1 Reply)
Discussion started by: mjoshi87
1 Replies

3. Shell Programming and Scripting

How to find the recent file in many sub-directories?

Hi guys, Under my root directory there are many sub-directories which contains log file for every day of running. How can I find , in one command only, the recent log file in each sub-directory? For example, If I run the following: find . -name "exp_prod_*_*_yes_*_.log" -exec ls -ltr {} \;... (12 Replies)
Discussion started by: nir_s
12 Replies

4. Shell Programming and Scripting

how to get the most recent file that contains a specific string

Hi All, I wish to get the most recent file from a dir which contains a specific string. for example, in a dir sample/ , i have 3 files file1.txt -- contains 'good' file2.txt -- contains 'good' file3.txt-- contans 'hello' I want to search for the recent file (that is file2.txt) which... (3 Replies)
Discussion started by: little_wonder
3 Replies

5. Shell Programming and Scripting

Getting the most recent file

Hi people, Please some help over here. I have logs in a directory, in which I need to get the most recent file in order to put it within other command. The format of the files are loadfiles20090308094339_41 loadfiles20090308094418_42 loadfiles20090308095457_43... (4 Replies)
Discussion started by: cgkmal
4 Replies

6. UNIX for Dummies Questions & Answers

sort biggest most recent files

if i am in /tmp file, and i have a few DIRs under /tmp. i want to find the biggest and most recent files (from 7 days ago) in /tmp and subfolders. (3 Replies)
Discussion started by: tjmannonline
3 Replies

7. UNIX for Dummies Questions & Answers

Get the most recent file from a remote server

Hi, This is an FTP related query.I hvae 2 files in a remote server with the timestamp attached to it. FilenameYYYYMMDDHHMMSS.EXT. I need to extract both the fileswith the most recent timestamp using the FTP script I have used : ftp Servername blah blah.... MOST_RECENT_FILE1=`ls -1t... (5 Replies)
Discussion started by: anujairaj
5 Replies

8. Shell Programming and Scripting

getting the most recent file

Hi, I have files coming in every day with that days timestamp like: nameyyyymmddhhmmss.ext. I need the most recent one and so i am using cat `ls -t name*|head -1 ` > temp i am sorting the files in the decending order and am copying the most recent one into a temp file. It works at times... (3 Replies)
Discussion started by: anujairaj
3 Replies

9. AIX

Changing name of most recent file

I need to change the name of a file within a script. For example I have two files. The first named scrnslc_0001 and the second scrnslc_0002. I want to change the name of the second to scrnslc_out. The appended number will change, and I won't know the file name in advance. If there were only one... (1 Reply)
Discussion started by: warpmail
1 Replies

10. Shell Programming and Scripting

Most Recent File script

OK, I know next to nothing about scripting in unix, and at the moment I don't have access to a unix environment... We have an application that generates a text report file which is later printed. The format is this: bbtptcYYMMDDSSCC.txt (year/month/day/second/check digit) I want a... (1 Reply)
Discussion started by: josborn777
1 Replies
Login or Register to Ask a Question