0

I have this file in my server that i need to get a specific range of rows using specified dates so i searched for a script and come up with this.

19 Sep 2018 08:47:57,153#ad****#L****#2018-Sep-19 08:47:43 110#2018-Sep-19 
08:47:57 153#14.***sec
19 Sep 2018 09:00:25,582#jg****#L****#2018-Sep-19 09:00:22 658#2018-Sep-19 
09:00:25 582#2.***sec
19 Sep 2018 09:00:25,720#lr****#L****#2018-Sep-19 09:00:22 038#2018-Sep-19 
09:00:25 720#3.***sec
....

I entered this command in the command line and it works well:
awk '$0 >= "19 Sep 2018 09:00:00" && $0 <= "19 Sep 2018 09:05:00" '/home/../k../g../somelogfile.txt

but when i manipulate it in the script and put variables in it there is no output.

todayl=$(date --date="19 Sept 2018 09:00:00AM")
t_i_f_o=$(date +"%d %b %Y %H:%M:%S" --date="$today")
t_f_m_a=$(date +"%d %b %Y %H:%M:%S" --date="$today + 5 minutes")

awk '$0 >= "$t_i_f_o" && $0 <= "$t_f_m_a" ' /home/m.../k.../t.../somelogfile1.txt > /home/m.../k.../t../f.../another1.txt #Tried this
awk '$0 >= "${t_i_f_o}" && $0 <= "${t_f_m_a}"' /home/m.../k.../t.../somefile2.txt > /home/m.../k.../t.../f.../another2.txt # and this

when i look at the output file which is another1.txt and another2.txt there is no data. is there something wrong with the way i place my variable?

rbcastro
  • 1
  • 2
  • Oww im sorry i mean 'dates' – rbcastro Sep 19 '18 at 09:27
  • Possible duplicate of [comparing dates using awk in bash](https://stackoverflow.com/questions/39947847/comparing-dates-using-awk-in-bash) – KamilCuk Sep 19 '18 at 09:29
  • im just wondering why when i manually put the dates in the awk command it works well. i think there is just something wrong with the way i pass the variable in the command itself. – rbcastro Sep 19 '18 at 09:36
  • Read about shell expansion [here](http://wiki.bash-hackers.org/syntax/quoting). Putting variables in `'` qoutes does not expand them. You need to put variables inside `"` qoutes, all the rest inside `'` qoutes. – KamilCuk Sep 19 '18 at 09:38

1 Answers1

2

You need to properly enclose your strings. You want the t_i_f_o variable to be expanded by bash, but you want $0 to be expanded by awk.
Also you need to set LC_ALL=C or LC_TIME=C otherwise you will get strange results on PC with different locales (as mine).

export LC_ALL=C
todayl=$(date --date="19 Sep 2018 09:00:00AM")
t_i_f_o=$(date +"%d %b %Y %H:%M:%S" --date="$todayl")
t_f_m_a=$(date +"%d %b %Y %H:%M:%S" --date="$todayl + 5 minutes")
awk '$0 >= "'"$t_i_f_o"'" && $0 <= "'"$t_f_m_a"'"' /tmp/1

But I "feel" like you should only compare the first field separated by '#' with date, not the whole line:

awk -F'#' '$1 >= "'"$t_i_f_o"'" && $1 <= "'"$t_f_m_a"'"' /tmp/1

Live version available on tutorialspoint.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • i have no problem with the dates, i echo the dates and it turned out as expected and i also tried the `awk -F'#' '$1 >= "'"$t_i_f_o"'" && $1 <= "'"$t_f_m_a"'"' /tmp/1` but same results :( – rbcastro Sep 19 '18 at 09:30
  • thanks this worked, the only reason that it didnt is that i forgot to delete the variable that im testing for string date. – rbcastro Sep 19 '18 at 09:43
  • Yes, I just discovered that too. It's `todayl`, not `today`. ; ) – KamilCuk Sep 19 '18 at 09:44
  • Don't do `awk -F'#' '$1 >= "'"$t_i_f_o"'" && $1 <= "'"$t_f_m_a"'"' /tmp/1`, do `awk -F'#' -v tifo="$t_i_f_o" -v tfma="$t_f_m_a" '$1 >= tifo && $1 <= tfma' /tmp/1` instead to avoid obscure errors if/when the shell variables contain unexpected values. – Ed Morton Sep 19 '18 at 17:35