'grep for a pattern "variable=value" and returning only matching entries having a value > threshold
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.
Solution 1:[1]
You didn't provide any sample input so it's a guess but this may be what you're trying to do:
awk -F'=' '($1=="variable") && ($2>300)' file
If that's not all you need then please edit your question to include a Minimal, Reproducible Example with concise, testable sample input, expected output and your attempt to solve the problem yourself so we can help you further. See [ask] and look at existing questions that have been upvoted and answered for examples.
Solution 2:[2]
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
Solution 3:[3]
I would use perl
, here I am printing the logs from a pod called bash-pod
. Filtering, only the lines with threshold=<integer-greater-than-80>
are present.
kubectl logs -f bash-pod |perl -ne '/.*threshold=(\d+).*/m;print if $1> 80'
The above code is tested with the below pod:
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: bash-pod
name: bash-pod
spec:
containers:
- image: bash
name: bash-pod
resources: {}
command: ['bash','-c','while true;do echo "threshold=$(( ( RANDOM % 100 ) + 1 ))";sleep 1;done']
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
Output:
kubectl logs -f bash-pod |perl -ne '/.*threshold=(\d+).*/m;print if $1> 80'
threshold=82
threshold=90
threshold=89
threshold=90
threshold=85
threshold=83
threshold=86
threshold=83
...
.....
Note: If you want to take float into consideration then you can use .*threshold=(\d+(?:\.\d+)?).*
as new regex.
Solution 4:[4]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Dominique |
Solution 3 | |
Solution 4 | RARE Kpop Manifesto |