0

im having trouble tou remove some specific lines from a file, but its not that simple. Tthe problem asks to search and then save the lines from a specific date and hour for example 2008-06-08 and 12:40:53.90 AM until the date that the user wants which is a read variable input $dayy. The real issue is that i want the lines that is greater after 12:40:53 until that if the $dayy is for example 2008-06-12 i need the lines that exist before the specific time 12:40:53.90, exactly after 4 days. i used the bash code below and with the last awk command i wanted to remove the lines that are in that day ($dayy) and their time are after the 12:40:53.90 but it removes all the lines in that date. if you could help i will really appreciate that.

for example if the initial day is 2008-06-08 (spec_date) and time 12:40:53.90 (timee) then i want all the lines from that day, time until the SAME time in two days 2008-06-10 (dayy)

read dayy ;  read spec_date ; read timee ;

awk -v dayy $dayy -v spec_date=$spec_date -v timee=$timee '{ if($1 >=spec_date && $2>=timee  && $1 <= dayy) print $0}' ex1.dat > ex2.dat

awk -v dayy=$dayy '! ( $1==dayy  &&   $2 ="[12-23]:[0-59]:[0-59].[0-50]")' ex2.dat

example of my data

2008-06-08  12:40:53.90  
2008-06-08  12:43:39.80 
.......................
2008-06-11  **12:39:34.22**
2008-06-11    13:21:23:43
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • The `awk` operator for matching a regular expression is `~`, not `=`. And `[0-59]` is not how you match a number in a range. – Barmar Mar 02 '20 at 20:47
  • See https://stackoverflow.com/questions/22130429/using-regular-expressions-to-validate-a-numeric-range for how to write a regexp for a numeric range. – Barmar Mar 02 '20 at 20:48

1 Answers1

0

There are 3 cases you need to match:

  1. First day: day == spec_date, match times >= timee
  2. Last day: day == dayy, match times <= timee
  3. In between: day between spec_date and dayy, match everything

So write each condition and combine them with ||.

awk -v dayy=$dayy -v spec_date=$spec_date -v timee=$timee '
    ($1 == spec_date && $2 >= timee) || 
    ($1 == dayy && $2 <= timee) || 
    ($1 > spec_date && $1 < dayy)' ex1.dat
Barmar
  • 741,623
  • 53
  • 500
  • 612