Need to filter the result set within 2 time frame

my sample file is like this

$cat onefile
05/21/18 13:10:07        ABRT US1CPDAY Status 1
05/21/18 21:18:54        ABRT DailyBackup_VFFPRDAPENTL01 Status 6
05/21/18 21:26:24        ABRT DailyBackup_VFFPRDAPENTL02 Status 6
05/21/18 21:57:36        ABRT DailyBackup_vm-ea1ffpreng01 Status 6
05/22/18 13:11:56        ABRT US1CPDAY Status 1
05/22/18 22:05:00        ABRT US1EXNITE Status 1
05/23/18 13:11:24        ABRT US1CPDAY Status 1
05/24/18 13:12:29        ABRT US1CPDAY Status 1
05/24/18 23:23:49        ABRT DailyBackup_IAM-FF-SMTP Status 156

i want to grep the lines between 05/22/18 to 05/23/18

I tried with sed

sed -n '/$first/,/$last/p' onefile

but it does not work.

I tried with grep -e

grep -e "05/22/18" -e "05/23/18" 

it too did not work complaining about illegal option -e

PLease help !!

how about - assuming the is sorted temporally and the to/from recods are present in the file...

awk -v f='05/21/18' -v t='05/23/18' '$1==f,$1==t' myFile
1 Like

sorry, but it did not work .
it says

awk: syntax error near line 1
awk: bailing out near line 1

Your statement describing what output you want is ambiguous. The only line between the first occurrence of 05/22/18 and 05/23/18 is the single line:

05/22/18 22:05:00        ABRT US1EXNITE Status 1

Is that what you want?

Saying that you tried something "but it does not work" does not give us much help in trying to guess what did not work. Did it produce diagnostic messages? Did it give no output? Did it give the wrong output?

What shell and operating system are you using? The command:

grep -e "05/22/18" -e "05/23/18"

should not complain about an invalid -e option. It should just wait for you to type in data to be processed and print out any lines that contain either of those strings (but, of course, that does not meet any requirements about finding lines between anything).

if on Solaris, use nawk...

The shell doesn't expand variables if enclosed in single quotes. Try double quotes:

sed -n "/$first/,/$last/p" onefile

Since the user's $first and $last both expand to strings containing slash ( / ) characters, double quotes won't work in this case either unless the slashes in the variables are escaped:

first='05\/22\/18'
last='05\/23\/18'

before invoking sed . And, if that is done, that sed command will print lines from the 1st one matching $first through the 1st one matching $last ; it won't print the lines between the patterns, it will also print the first line match each of those patterns.

You may try e.g.

sed -n "\#$first#,\#$last#p" file

, then.