简体   繁体   中英

How to Start and Stop printing lines with Specific Text in Linux/Unix Terminal

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

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, eg

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, eg

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.

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
sed -n '/Start$/,/End$/p' input

To filter on today's date:

sed -n "\@$(date +%Y/%m/%d)@"'{ /Start$/,/End$/p; }' input

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM