0

Print file on terminal Starting and Ending in a line with specific Text

For Example: file.txt

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.....

I tried to display the lines with grep cmd, but I am unable to the required output.

Expected Result: Command to get the results based on system date

> some command in grep (or) awk (or) sed with system date for current date

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
Naga
  • 347
  • 2
  • 16
  • 1
    https://www.google.com/search?q=bash+how+to+filter+lines+between+patterns https://stackoverflow.com/questions/17988756/how-to-select-lines-between-two-marker-patterns-which-may-occur-multiple-times-w – KamilCuk Oct 20 '19 at 09:33
  • 1
    Possible duplicate of [Bash, grep between two lines with specified string](https://stackoverflow.com/questions/22221277/bash-grep-between-two-lines-with-specified-string) – KamilCuk Oct 20 '19 at 09:34

3 Answers3

0

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.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • Thanks for your reply. @David C.Rankin. How to edit awk to include current system date? I want my command to pick up current date from column 1 – Naga Oct 20 '19 at 11:06
  • Isn't that what was done? you have the date `2019/10/17` in the output -- or are you saying forget the date in the input file and just replace it by the current system date? – David C. Rankin Oct 20 '19 at 11:47
  • Yes everyday date will change, I need the command to pick up the current date automatically and show up the lines only for the current day. For example if I run the command today it should show up the start and end portion of lines for 2019/10/20, if I run it tomorrow it should show up the start and end lines for 2019/10/21 – Naga Oct 20 '19 at 11:56
0

Here is an awk script. With explanation: It will solve for printing partial terminating sections as well.

script.awk

BEGIN {FS=" -I- "}  # Field separator is " -I- ", therefore idicative field data is in fiedl $2
$2 ~ "^Start"{onSection = 1} # if indicative field start with "Start", mark section flag ON
onSection{print} # print output only if marked section flag ON
$2 ~"^End"{onSection = 0} # if indicative field ends with "End", mark section flag OFF

running

awk -f script.awk file.txt

or one liner:

awk 'BEGIN{FS=" -I- "}$2~"^Start"{f=1}f{print}$2~"^End"{f=0}' file.txt

output

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
Dudi Boy
  • 4,551
  • 1
  • 15
  • 30
0
sed -n '/Start$/,/End$/p' input

To filter on today's date:

sed -n "\@$(date +%Y/%m/%d)@"'{ /Start$/,/End$/p; }' input
William Pursell
  • 204,365
  • 48
  • 270
  • 300