'How to grep and exclude "nn/nn" matches
I have the following example data (from the output of 'command'):
field1 field2 1/1 field4
field1 field2 2/2 field4
field1 field2 10/10 field4
field1 field2 5/7 field4
I'd like to only display the lines where the first number does not match the second number in field3.
I have this grep command which works fine when the numbers are a single digits:
command | grep -Pv '\s+([1-9]+)\/\1\s+'
So when I use the grep above with the example data, I get the following displayed:
field1 field2 10/10 field4
field1 field2 5/7 field4
Ideally I want the line with 10/10 also matched and excluded.
Any ideas?
Cheers
Solution 1:[1]
1st solution: This could be done with a simple awk. Simple explanation would be, setting up field separator as space(s) OR / for all the lines. In main program checking if 3rd field is equal to 4th field then print that line.
your_command | awk -F'[[:space:]]+|/' '$3!=$4'
2nd solution: With your shown samples, please try following awk program.
your_command | awk '{split($3,arr,"/");if(arr[1]!=arr[2]){print}}'
Solution 2:[2]
you can use the below command:
kubectl get pod |grep -P '\s+([1-9]+[\d]*)\/\1\s+'
the command provided in the question was failing the edge case due to the handling of completed pods. Which is updated here.
Example:
kubectl get pod
NAME READY STATUS RESTARTS AGE
app_1 1/1 Running 0 77m
app_2 1/1 Running 0 77m
app_3 0/1 Completed 0 77m
app_4 1/1 Running 0 77m
app_5 8/8 Running 0 77m
app_6 4/4 Running 1 77m
app_7 1/1 Running 0 77m
app_8 1/1 Running 0 77m
app_9 1/1 Running 0 77m
app_10 1/1 Running 0 77m
app_11 1/1 Running 0 77m
app_12 1/1 Running 0 77m
app_13 1/1 Running 0 75m
app_14 2/2 Running 0 77m
app_15 2/2 Running 0 77m
app_16 2/2 Running 0 76m
app_17 4/8 Running 0 77m
app_18 1/1 Running 0 77m
app_19 1/1 Running 0 77m
app_20 1/1 Running 0 77m
app_21 1/1 Running 0 77m
app_22 2/2 Running 0 77m
app_23 3/3 Running 0 77m
app_24 1/1 Running 0 77m
app_25 1/1 Running 0 77m
app_26 1/1 Running 0 77m
app_27 10/10 Running 0 77m
app_28 2/2 Running 0 77m
Ready pods:
kubectl get pod |grep -P '\s+([1-9]+[\d]*)\/\1\s+'
app_1 1/1 Running 0 77m
app_2 1/1 Running 0 77m
app_4 1/1 Running 0 77m
app_5 8/8 Running 0 77m
app_6 4/4 Running 1 77m
app_7 1/1 Running 0 77m
app_8 1/1 Running 0 77m
app_9 1/1 Running 0 77m
app_10 1/1 Running 0 77m
app_11 1/1 Running 0 77m
app_12 1/1 Running 0 77m
app_13 1/1 Running 0 75m
app_14 2/2 Running 0 77m
app_15 2/2 Running 0 77m
app_16 2/2 Running 0 76m
app_18 1/1 Running 0 77m
app_19 1/1 Running 0 77m
app_20 1/1 Running 0 77m
app_21 1/1 Running 0 77m
app_22 2/2 Running 0 77m
app_23 3/3 Running 0 77m
app_24 1/1 Running 0 77m
app_25 1/1 Running 0 77m
app_26 1/1 Running 0 77m
app_27 10/10 Running 0 77m
app_28 2/2 Running 0 77m
Not Ready pods:
kubectl get pod|grep -Pv '\s+([1-9]+[\d]*)\/\1\s+'
NAME READY STATUS RESTARTS AGE
app_3 0/1 Completed 0 77m
app_17 4/8 Running 0 77m
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 | P.... |
