Solved

Sort a file with date - linux

Posted on 2016-11-03
7
22 Views
Last Modified: 2016-11-09
I have a file which contents below data (this file is date-sorted already , you can see first column)

2015-03-19 billing-gateway
2015-03-23 btnetdesign-greenfield
2015-06-01 bigbird1
2015-10-07 bigbird
2015-11-03 expedioomrm
2016-02-17 RositaSR
2016-03-23 ocsnet
2016-06-29 or-vmj
2016-06-30 perftest1_200
2016-06-30 perftest2_0
2016-06-30 perftest3_25
2016-06-30 perftest4_500
2016-06-30 perftest5_1000
2016-10-03 admin
2016-10-03 test-repo-03102016-2
2016-10-17 cmps
2016-11-02 VMM_Genesys

Need to display rows which are before 6 months date
like today we have 03/11/2016 then need to extract those rows only which are older than 2016-05-03
So in above data , from 2016-03-23 ocsnet backwards dates all rows

So logically it should calculate with current date and throw out the older than six months rows only
0
Comment
Question by:Ravi Raj
7 Comments
 
LVL 76

Expert Comment

by:arnold
ID: 41872580
Using perl, timelocal convert the date string to UNIX timestamp since epoch.
Then you can do logical test 24*3600*182
Use time to get current timestamp, ...........
1
 
LVL 26

Expert Comment

by:wilcoxon
ID: 41872667
Another alternative is simply to pattern match.  Here's how you'd do it in perl.
use strict;
use warnings;
my ($yr, $mon, $day) = (localtime)[5,4,3];
$mon -= 5;  # start with the full month to be excluded
if ($mon < 1) {
    $mon += 12;
    $yr--;
}
my $mrx = "$yr-";
if ($mon < 10) {
    $mrx = "(?:0(?:$mon-9)|1[0-2])-";
} else {
    my $tmp = $mon - 10;
    $mrx = "1[$tmp-2]-";
}
$mon--;
if ($mon < 1) {
    $mon += 12;
    $yr--;
}
my $drx;
if ($day < 10) {
    $drx = "(?:0[$day-9]|[123][0-9])";
} else {
    my @dig = split //, $day;
    my $nxt = $dig[0]+1;
    $drx = "(?:$dig[0][$dig[1]-9]|[$nxt-3][0-9])";
}
$drx = sprintf '%d-%02d-%s', $yr, $mon, $drx;
open IN, 'input_file.log' or die "could not open input: $!";
while (<IN>) {
    last if (m{^$mrx} or m{^$drx});
    print;
}

Open in new window

1
 

Accepted Solution

by:
Ravi Raj earned 0 total points
ID: 41873938
Thanks Arnold , Wilcoxon
I wrote a simple perl which works perfect for me

use Time::Piece;
$num_args = $#ARGV + 1;
if ($num_args eq 1) {
    $entryDate = $ARGV[0];
        while (defined(my $file = glob 'myfile.csv')) {
  open my $fh, "<", $file;  # lexical file handles, automatic error handling

  LINE: while (defined( my $line = <$fh> )) {

                s/, /,/;
                s/\"[^\"]+"//;
                @x = split(" ", $line);
                $date = $x[0];
$date1 = Time::Piece->strptime($date, "%Y-%m-%d");
$date2 = Time::Piece->strptime($entryDate, "%Y-%m-%d");

                if($date1 < $date2) {
print $line;
                }
}
}
}
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 13

Expert Comment

by:F Igor
ID: 41873952
To Get the min date

MINDATE=$(date -d "-6 months" +%Y-%m-%d

Read all lines and only print lines "greater" than MINDATE:

for line in (names.txt)
do
   if ( "$line" > "$MINDATE" ) ; then
     echo $line
  fi
done

Open in new window

0
 
LVL 26

Expert Comment

by:skullnobrains
ID: 41874109
if your file is sorted, you probably should not check the dates for each line : either read the file until you find the fist date that is more recent that 6 months and exit, or read it in reverse order and start printing everything after the first date

you can code this in pure shell

see this example that reads the file in reverse order
$ tac /tmp/in | { while read date line ; do test $(date --date "$date"  +%s) -gt $(date --date '-1 month' +%s ) && break ; done ; cat ; }
2016-10-17 cmps
2016-10-03 test-repo-03102016-2
2016-10-03 admin
2016-06-30 perftest5_1000
2016-06-30 perftest4_500
2016-06-30 perftest3_25
2016-06-30 perftest2_0
2016-06-30 perftest1_200
2016-06-29 or-vmj
2016-03-23 ocsnet
2016-02-17 RositaSR
2015-11-03 expedioomrm
2015-10-07 bigbird
2015-06-01 bigbird1

Open in new window


or in actual order
$ cat /tmp/in | while read date line ; do test $(date --date "$date"  +%s) -lt $(date --date '-1 month' +%s ) || exit ; echo $date $line ; done
2015-06-01 bigbird1
2015-10-07 bigbird
2015-11-03 expedioomrm
2016-02-17 RositaSR
2016-03-23 ocsnet
2016-06-29 or-vmj
2016-06-30 perftest1_200
2016-06-30 perftest2_0
2016-06-30 perftest3_25
2016-06-30 perftest4_500
2016-06-30 perftest5_1000
2016-10-03 admin
2016-10-03 test-repo-03102016-2

Open in new window

0
 
LVL 76

Expert Comment

by:arnold
ID: 41874328
Why not award points to Wilcoxon, myself and others whose suggestions helped you resolve the issue?
1
 

Author Closing Comment

by:Ravi Raj
ID: 41880269
Wrote my own script
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now