Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

Jason Lun

IS-IT--Management
Feb 18, 2020
2
CA
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.
 
Hi

Use range pattern :
man awk said:
Patterns
AWK patterns may be one of the following:
[gray](...)[/gray]
pattern1, pattern2

[gray](...)[/gray]
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:
awk '[fuchsia]/Requests in progress/[/fuchsia][teal]&&[/teal][navy]$NF[/navy][teal]>=[/teal][purple]10[/purple][teal],[/teal][navy]$0[/navy][teal]==[/teal][i][green]"<<<<<"[/green][/i]' file.log
( I changed the condition abit as your also matched these :
[tt][2018-02-12--16-32-09-230--XkR86Qrc3AQAAOoXT-0AAAJU--App.ts=68
[2018-02-12--16-34-09-254--XkR9YQrc3AQAAQY1AgoAAAAA--App.ts=164[/tt] )

Unfortunately excluding the delimiters is not really elegant :
Code:
awk '[fuchsia]/Requests in progress/[/fuchsia][teal]&&[/teal][navy]$NF[/navy][teal]>=[/teal][purple]10[/purple][teal],[/teal][navy]$0[/navy][teal]==[/teal][i][green]"<<<<<"[/green][/i][teal]{[/teal][b]if[/b][teal]([/teal][navy]$0[/navy][teal]!=[/teal][i][green]">>>>>"[/green][/i][teal]&&[/teal][navy]$0[/navy][teal]!=[/teal][i][green]"<<<<<"[/green][/i][teal])[/teal][b]print[/b]}' file.log

Feherke.
feherke.github.io
 
Thank you feherke, you made my day! That explains a lot and is exactly what I was hoping for, thank you again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top