I am searching a kubernetes pod logs for the pattern “variable=value” ( e.g., variable=10 or variable=500) using the command below:
Kubectl logs -f | grep “variable=”
My question is that wether it is possible to modify the command above in order to return the logs where the variable value is greater than some threshold, e.g, for threshold=300
, variable=301
, variable=302
would be filetered in, but variable=299
would be filtered out
I know I can develop a small program for this, but rather I want a rapid solution in the command line direcly without the hassle of writing a small prgram.
CodePudding user response:
As a test, I've created this file:
Prompt> cat test.txt
var=2
var=22
var=222
blabla
First, I filter our the variable assignment lines:
Prompt> grep "var=" test.txt
var=2
var=22
var=222
Then, I filter on the condition of the values, in two ways:
Prompt> grep "var=" test.txt | awk -F '=' '{if ($2 > 25) print $1 "=" $2}'
var=222
Prompt> grep "var=" test.txt | awk -F '=' '{if ($2 < 25) print $1 "=" $2}'
var=2
var=22
CodePudding user response:
Here's a flexible solution that allows you specify parameters you like, and functions the same :
< test_filter_threshold.txt |
mawk 'NF*=(____=="<")!=((___~"."? ___:___\
)<= $(NF*=(!_<NF)*(__==$!_)))' FS== OFS== \
__="var" ___="1" ____=">="
var=2
var=22
var=222
___=4 ____='<'
var=2
___=200 ____='<'
var=2
var=22
___=200 ____='>='
var=222
___=200 ____= # "<" is strictly less than,
# all else def. to ">="
var=222
___='3.14159' ____='>=' # float point thresholds (TRHD) are valid
var=22
var=222
___= ____= # missing TRHD def. to all
var=2
var=22
var=222
___='abc' ____='>=' # invalid TRHD def. to non-zero positive
var=2
var=222
1 var=2
2 var=-22
3 var=222
4 blabla