Home > Software engineering >  How to convert awk match to powershell command?
How to convert awk match to powershell command?

Time:12-18

I am currently running this command on a linux machine to get pods older than 1 day:

kubectl get pod | awk 'match($5,/[0-9] d/) {print $1}'

I want to be able to run the same command in Powershell. I could I do it?

Output of kubectl get pod:

        NAME       READY     STATUS      RESTARTS      AGE
        pod-name   1/1       Running     0             2h3m
        pod-name2  1/1       Running     0             1d2h
        pod-name3  1/1       Running     0             4d4h

Output of kubectl get pod | awk 'match($5,/[0-9] d/) {print $1}'

    pod-name2
    pod-name3

CodePudding user response:

You can use:

$long_running_pods=(kubectl get pod | Tail -n 2 | ConvertFrom-String -PropertyNames NAME, READY, STATUS, RESTARTS, AGE | Where-Object {$_.AGE -match "[1-9][0-9]*d[0-9]{1,2}h"})
$long_running_pods.NAME

This will give you all pods which have been running for more than one day.


Example:

$long_running_pods=(echo 'NAME       READY     STATUS      RESTARTS      AGE
pod-name   1/1       Running     0             1d2h
pod-name2  1/1       Running     0             0d0h' | Tail -n 2 | ConvertFrom-String -PropertyNames NAME, READY, STATUS, RESTARTS, AGE | Where-Object {$_.AGE -match "[1-9][0-9]*d[0-9]{1,2}h"})
$long_running_pods.NAME 

will print:

pod-name
  • Related