Solved

Sort a file with date - linux

Posted on 2016-11-03
7
43 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 78

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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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 27

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 78

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The viewer will learn how to count occurrences of each item in an array.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

726 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