×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

AWK Question: Printing a range after a condition is met..

AWK Question: Printing a range after a condition is met..

AWK Question: Printing a range after a condition is met..

(OP)
I am in the process of learning AWK and looking to print some data in a range, only if a certain prerequisite field condition is met. Consider the following sample data from file.log:

[2018-02-12 16:31:09.236 host.domain INFO {97537}] Requests in progress: 0
>>>>>
<<<<<
[2018-02-12 16:32:09.296 host.domain INFO {97537}] Requests in progress: 1
>>>>>
[2018-02-12--16-32-09-230--XkR86Qrc3AQAAOoXT-0AAAJU--App.ts=68
<<<<<
[2018-02-12 16:33:09.357 host.domain INFO {97537}] Requests in progress: 0
>>>>>
<<<<<
[2018-02-12 16:34:09.418 host.domain INFO {97537}] Requests in progress: 1
>>>>>
[2018-02-12--16-34-09-254--XkR9YQrc3AQAAQY1AgoAAAAA--App.ts=164
<<<<<
[2018-02-12 16:35:09.479 host.domain INFO {97537}] Requests in progress: 0
>>>>>
<<<<<
[2018-02-12 16:36:09.540 host.domain INFO {97537}] Requests in progress: 0
>>>>>
<<<<<
[2018-08-03 13:22:17.662 host.domain INFO {97537}] Requests in progress: 13
>>>>>
2018-08-03--13-18-55-228--XkrnHwrc3AQAABIILK8AAAJK--App.ts=202424
2018-08-03--13-19-18-389--XkrnNgrc3AQAAFePAbgAAABJ--App.ts=179263
2018-08-03--13-19-22-580--XkrnOgrc3AQAABZ7n6sAAAKN--App.ts=175073
2018-08-03--13-19-57-629--XkrnXQrc3AQAAHJps8MAAAEN--App.ts=140024
2018-08-03--13-20-38-385--Xkrnhgrc3AQAAC8IRFUAAAAV--App.ts=99268
2018-08-03--13-20-39-571--Xkrnhwrc3AQAABIF-GIAAAFT--App.ts=98082
2018-08-03--13-21-03-828--Xkrnnwrc3AQAABZ7oDwAAAKU--App.ts=73824
2018-08-03--13-21-34-179--Xkrnvgrc3AQAABIF-K4AAAFM--App.ts=43474
2018-08-03--13-21-34-491--Xkrnvgrc3AQAABIHyHwAAAIH--App.ts=43161
2018-08-03--13-21-35-482--Xkrnvwrc3AQAAX9IOogAAANC--App.ts=42171
2018-08-03--13-22-17-285--Xkrn6Qrc3AQAAX-mZFUAAAOQ--Mover.ts=367
2018-08-03--13-22-17-319--Xkrn6Qrc3AQAAMaqSowAAAGX--Mover.ts=334
2018-08-03--13-22-17-638--Xkrn6Qrc3AQAABIHyJQAAAIS--Mover.ts=15
<<<<<
[2018-08-03 13:23:17.724 host.domain INFO {97537}] Requests in progress: 19
>>>>>
2018-08-03--13-18-55-228--XkrnHwrc3AQAABIILK8AAAJK--App.ts=262496
2018-08-03--13-19-18-389--XkrnNgrc3AQAAFePAbgAAABJ--App.ts=239335
2018-08-03--13-19-22-580--XkrnOgrc3AQAABZ7n6sAAAKN--App.ts=235145
2018-08-03--13-19-57-629--XkrnXQrc3AQAAHJps8MAAAEN--App.ts=200096
2018-08-03--13-20-38-385--Xkrnhgrc3AQAAC8IRFUAAAAV--App.ts=159340
2018-08-03--13-20-39-571--Xkrnhwrc3AQAABIF-GIAAAFT--App.ts=158154
2018-08-03--13-21-03-828--Xkrnnwrc3AQAABZ7oDwAAAKU--App.ts=133896
2018-08-03--13-21-34-179--Xkrnvgrc3AQAABIF-K4AAAFM--App.ts=103546
2018-08-03--13-21-34-491--Xkrnvgrc3AQAABIHyHwAAAIH--App.ts=103233
2018-08-03--13-21-35-482--Xkrnvwrc3AQAAX9IOogAAANC--App.ts=102243
2018-08-03--13-22-28-622--Xkrn9Arc3AQAAHJptG4AAAED--App.ts=49102
2018-08-03--13-22-35-159--Xkrn@wrc3AQAAHJptHQAAAEU--App.ts=42566
2018-08-03--13-22-43-351--XkroAwrc3AQAABIHyLEAAAIK--App.ts=34373
2018-08-03--13-22-49-016--XkroCQrc3AQAABIF-PAAAAFL--App.ts=28708
2018-08-03--13-22-51-626--XkroCwrc3AQAABIHyL0AAAIP--App.ts=26097
2018-08-03--13-23-02-371--XkroFgrc3AQAAXcCgAkAAADD--App.ts=15354
2018-08-03--13-23-13-733--XkroIQrc3AQAAX-mZM8AAAOF--App.ts=3992
2018-08-03--13-23-16-514--XkroJArc3AQAAAyr2DIAAAME--App.ts=1210
2018-08-03--13-23-17-707--XkroJQrc3AQAAWUa5a0AAALJ--App.ts=18
<<<<<
[2018-08-03 13:24:17.791 host.domain INFO {97537}] Requests in progress: 29
>>>>>
2018-08-03--13-18-55-228--XkrnHwrc3AQAABIILK8AAAJK--App.ts=322561
2018-08-03--13-19-18-389--XkrnNgrc3AQAAFePAbgAAABJ--App.ts=299400
2018-08-03--13-19-22-580--XkrnOgrc3AQAABZ7n6sAAAKN--App.ts=295210
2018-08-03--13-19-57-629--XkrnXQrc3AQAAHJps8MAAAEN--App.ts=260161
2018-08-03--13-20-38-385--Xkrnhgrc3AQAAC8IRFUAAAAV--App.ts=219405
2018-08-03--13-20-39-571--Xkrnhwrc3AQAABIF-GIAAAFT--App.ts=218219
2018-08-03--13-21-03-828--Xkrnnwrc3AQAABZ7oDwAAAKU--App.ts=193961
2018-08-03--13-21-34-179--Xkrnvgrc3AQAABIF-K4AAAFM--App.ts=163611
2018-08-03--13-21-34-491--Xkrnvgrc3AQAABIHyHwAAAIH--App.ts=163298
2018-08-03--13-21-35-482--Xkrnvwrc3AQAAX9IOogAAANC--App.ts=162308
2018-08-03--13-22-28-622--Xkrn9Arc3AQAAHJptG4AAAED--App.ts=109167
2018-08-03--13-22-35-159--Xkrn@wrc3AQAAHJptHQAAAEU--App.ts=102631
2018-08-03--13-22-43-351--XkroAwrc3AQAABIHyLEAAAIK--App.ts=94438
2018-08-03--13-22-49-016--XkroCQrc3AQAABIF-PAAAAFL--App.ts=88773
2018-08-03--13-22-51-626--XkroCwrc3AQAABIHyL0AAAIP--App.ts=86162
2018-08-03--13-23-02-371--XkroFgrc3AQAAXcCgAkAAADD--App.ts=75419
2018-08-03--13-23-13-733--XkroIQrc3AQAAX-mZM8AAAOF--App.ts=64057
2018-08-03--13-23-16-514--XkroJArc3AQAAAyr2DIAAAME--App.ts=61275
2018-08-03--13-23-22-313--XkroKgrc3AQAAXcCgHcAAADY--App.ts=55477
2018-08-03--13-23-44-484--XkroQArc3AQAAC81pNEAAACV--App.ts=33304
2018-08-03--13-23-44-539--XkroQArc3AQAAFePA34AAABH--App.ts=33250
2018-08-03--13-23-44-715--XkroQArc3AQAAXcCgJQAAADR--App.ts=33074
2018-08-03--13-23-45-083--XkroQQrc3AQAABIILlYAAAJO--App.ts=32706
2018-08-03--13-23-46-094--XkroQgrc3AQAAC81pNIAAACY--App.ts=31695
2018-08-03--13-24-01-244--XkroUQrc3AQAABIILm8AAAJS--App.ts=16545
2018-08-03--13-24-06-048--XkroVgrc3AQAAX9IO1wAAANK--App.ts=11742
2018-08-03--13-24-06-341--XkroVgrc3AQAAHJptSEAAAEM--App.ts=11449
2018-08-03--13-24-06-360--XkroVgrc3AQAAXcCgMwAAADH--App.ts=11429
2018-08-03--13-24-12-436--XkroXArc3AQAAX-mZUMAAAOA--App.ts=5353
<<<<<

The goal here is to find and print all line sections with "Requests in progress" that are > or = 10, print those lines below until their endings (at <<<<<) and preferably, exclude printing ">>>>>" and/or "<<<<<". I would prefer to do it in one line, but in a script is also fine.

So far, I have this working but cannot figure out how to print the next lines based on these conditions, using another pattern search:

Here I found what I want to print, except the lines in the sections below these:
$ awk '/^\[/ {if($NF>=10) print}' Requests
[2018-08-03 13:22:17.662 host.domain INFO {97537}] Requests in progress: 13
[2018-08-03 13:23:17.724 host.domain INFO {97537}] Requests in progress: 19
[2018-08-03 13:24:17.791 host.domain INFO {97537}] Requests in progress: 29

Any help figuring this out would be greatly appreciated.

RE: AWK Question: Printing a range after a condition is met..

Hi

Use range pattern :

Quote (man awk)

Patterns
AWK patterns may be one of the following:
(...)
pattern1, pattern2

(...)
The pattern1, pattern2 form of an expression is called a range pattern. It matches all input records starting with a record that matches pattern1, and continuing until a record that matches pattern2, inclusive. It does not combine with any other sort of pattern expression.

The simplest is just this :

CODE --> command line

awk '/Requests in progress/&&$NF>=10,$0=="<<<<<"' file.log 
( I changed the condition abit as your also matched these :
[2018-02-12--16-32-09-230--XkR86Qrc3AQAAOoXT-0AAAJU--App.ts=68
[2018-02-12--16-34-09-254--XkR9YQrc3AQAAQY1AgoAAAAA--App.ts=164
)

Unfortunately excluding the delimiters is not really elegant :

CODE --> command line

awk '/Requests in progress/&&$NF>=10,$0=="<<<<<"{if($0!=">>>>>"&&$0!="<<<<<")print}' file.log 

Feherke.
feherke.github.io

RE: AWK Question: Printing a range after a condition is met..

(OP)
Thank you feherke, you made my day! That explains a lot and is exactly what I was hoping for, thank you again!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close