0

I have below input and I want to select lines with dates from now to 2 weeks or 3 weeks and so on.

0029L5 08/19/2017 00:57:33
0182L5 08/19/2017 05:53:57
0183L5 02/17/2018 00:00:16
0091L5 10/19/2022 00:00:04
0045L5 07/27/2017 09:03:56
0059L5 08/14/2017 00:51:50
0100L5 08/20/2017 01:25:39
0111L5 08/21/2017 00:46:15
0128L5 08/21/2017 12:38:51
D00054 07/21/2017 09:01:19

So the desired output if let say I want for 2 weeks from now

0045L5 07/27/2017 09:03:56
D00054 07/21/2017 09:01:19

But if i want for let say 4 weeks then the output should be

0045L5 07/27/2017 09:03:56
0059L5 08/14/2017 00:51:50
D00054 07/21/2017 09:01:19
user2314737
  • 27,088
  • 20
  • 102
  • 114
Sid
  • 161
  • 1
  • 10
  • 1
    definitely not sed. What have you tried, and where are you stuck? – glenn jackman Jul 21 '17 at 13:13
  • is `now` date always the last line? – RomanPerekhrest Jul 21 '17 at 13:38
  • I am trying to use Guru's solution for AIX, I am trying to calculate epoch time as mktime is not supported in AIX awk, However I am not getting any formula to apply to convert a given date into epoch time for comparison – Sid Jul 26 '17 at 20:51
  • It might be easier to use some other scripting language like Python or Perl. – user2314737 Jul 26 '17 at 20:57
  • I can use perl to calculate epoch time and use it to compare with current epoch time. I am trying to split column 2 & 3 to get $sec,$min,$hour,$mday$month$year, So can calculate epoch time using perl -e 'use Time::Local; print timegm(56,3,9,27,07-1,2017), "\n";' – Sid Jul 27 '17 at 02:50

3 Answers3

0

One way:

awk '{split($2,a,"/");split($3,b,":"); x=mktime(a[3]" "a[1]" "a[2]" "b[1]" "b[2]" "b[3]);y=systime();}x>y && x<(y+(n*7*24*60*60))' n=2 file

where n indicates the number of weeks

split($2,a,"/") => Split the 2nd column on the basis of / and store in array a

split($3,b,":") => Split the 3rd column on the basis of : and store in array b

mktime => gives the time in seconds

x contains the time in file in seconds

y contains the current time in seconds

Guru
  • 16,456
  • 2
  • 33
  • 46
  • mktime and systime are not supported date functions in AIX, I am trying to interpret times into epoch to compare.Still stuck – Sid Jul 26 '17 at 19:51
  • trying to convert the date into epoch time to compare with date +%s, However I am not getting any mathematical formula to do so, as date -d ' ' +%s is not supported in AIX – Sid Jul 26 '17 at 20:49
0

Here's one solution using bash where file is the name of your file:

while read r; do dd=$(($(date -d "${r:6}" +%s) - $(date +%s))); echo $(($dd/(3600*24))); done < file

This will compute the date difference in seconds between the date in ${r:6} (substring of the current row) and today's date $(date +%s) and convert it to days.

To output only lines where the date difference is less than 2 weeks (1209600 seconds)

while read r; do dd=$(($(date -d "${r:6}" +%s) - $(date +%s)));  if [ "$dd" -lt 1209600 ]; then echo $r; fi; done < file
user2314737
  • 27,088
  • 20
  • 102
  • 114
0

This works fine, Please let me know in case anybody has any other simpler solution for AIX.

awk '{split($2,a,"/");split($3,b,":"); print $1,b[3],b[2],b[1],a[2],a[1],a[3]}' /tmp/TLD_1 | head -10 | while read media sec min hour day mon year;  do month=$((10#$mon-1)); expiry=$(perl -e 'use Time::Local; print timegm(@ARGV[0,1,2,3], $ARGV[4], $ARGV[5]), "\n";' $sec $min $hour $day $month $year); current=$(date +%s); twoweeks=$(($current + (2*7*24*60*60))); if [ "$expiry" -gt "$current" -a "$expiry" -lt "$twoweeks" ]; then echo "$media $mon/$day/$year $hour:$min:$sec"; fi; done
Sid
  • 161
  • 1
  • 10
  • If you can use perl then just do the whole thing in perl. If you create a question and tag it with perl I'm sure you'd get an answer. – Ed Morton Aug 28 '17 at 19:11