A simple way in awk
is just keep a flag and set n = 1
when "Start"
is encountered in field 4. Checking n == 1
gives you a test to print each line while n
is 1
. When "End"
is reached, simply print that line and exit, e.g.
awk '$4 == "End" {print; exit} $4 == "Start" {n = 1} n == 1' file
(note: the operation associated with the rule n == 1
is simply the default operation of print
. It is equivalent to writing n == 1 {print}
)
Example Use/Output
Using your data file in file
, you would get:
$ awk '$4 == "End" {print; exit} $4 == "Start" {n = 1} n == 1' file
2019/10/17 23:01:02 -I- Start
2019/10/17 23:01:02 -I- ======================
2019/10/17 23:05:02 -I- Summary Details
2019/10/17 23:10:02 -I- End
It can also be written as:
awk '$4 == "Start" {n = 1} n == 1; $4 == "End" {exit}' file
Edit - Todays Date Only
If you want to match today's date only, you simply need to pass the date in the format you need as a variable to awk
from the shell using the -v
option, e.g.
awk -v date="$(date +%Y/%m/%d)" '
$1 == date {
if($4 == "Start") n = 1
if(n == 1) {print}
if($4 == "End") exit
}
' file
Example Input
$ cat file
2019/10/17 23:01:02 -W- Blah Blah Blah.....
2019/10/17 23:01:02 -I- Blah Blah Blah.....
2019/10/17 23:01:02 -I- Blah Blah Blah.....
2019/10/17 23:01:02 -I- Start
2019/10/17 23:01:02 -I- ======================
2019/10/17 23:05:02 -I- Summary Details
2019/10/17 23:10:02 -I- End
2019/10/17 23:10:02 -I- Blah Blah Blah.....
2019/10/17 23:10:02 -I- Blah Blah Blah.....
2019/10/17 23:10:02 -I- Blah Blah Blah.....
2019/10/20 23:01:02 -W- Blah Blah Blah.....
2019/10/20 23:01:02 -I- Blah Blah Blah.....
2019/10/20 23:01:02 -I- Blah Blah Blah.....
2019/10/20 23:01:02 -I- Start
2019/10/20 23:01:02 -I- ======================
2019/10/20 23:05:02 -I- Summary Details
2019/10/20 23:10:02 -I- End
2019/10/20 23:10:02 -I- Blah Blah Blah.....
2019/10/20 23:10:02 -I- Blah Blah Blah.....
2019/10/20 23:10:02 -I- Blah Blah Blah.....
Example Use/Output (for 2019/10/20)
$ awk -v date="$(date +%Y/%m/%d)" '
> $1 == date {
> if($4 == "Start") n = 1
> if(n == 1) {print}
> if($4 == "End") exit
> }
> ' file
2019/10/20 23:01:02 -I- Start
2019/10/20 23:01:02 -I- ======================
2019/10/20 23:05:02 -I- Summary Details
2019/10/20 23:10:02 -I- End
Let me know if you have further questions.