0

Team, my below command, greps for any pods with problems then takes them one by one and deletes.

But I want to use only first 10 lines of my command output.

kubectl get pods --all-namespaces | grep -i -e Evict -e Error | 
    awk -F ' ' '{print $1, $2, $4}' | 
    xargs -l1 -- sh -c 'kubectl delete pod "$2" -n "$1"' --

above command is deleting for all rows but i want to do only for first 10 rows. any hint?

i tried

kubectl get pods --all-namespaces | grep -i -e Evict -e Error | 
    awk -F ' ' '{print $1, $2, $4}' | for run{1..10}; do 
        xargs -l1 -- sh -c 'kubectl delete pod "$2" -n "$1" ' --; 
    done

once, i know this, i can use it for any command node or pod.

AhmFM
  • 1,552
  • 3
  • 23
  • 53
  • Can you provide the output of the command `kubectl get pods --all-namespaces` without any pipes? – steve Dec 18 '18 at 00:56

1 Answers1

1

You can use head to get the first 10 lines of output before passing to xargs. Use the -n option to specify how many lines (in this case, head -n10)

Just before piping to xargs, insert the following: | head -n10 |. This will filter everything but the first ten lines of preceding output.

Try this:

kubectl get pods --all-namespaces | grep -i -e Evict -e Error | awk -F ' ' '{print $1, $2, $4}' | head -n10 | xargs -l1 -- sh -c 'kubectl delete pod "$2" -n "$1"' --

steve
  • 352
  • 3
  • 11
  • 1
    awesome. worked.. i just did this: with --no-headers kubectl get nodes | awk -F ' ' '{print $1}' | head -n2 | kubectl get nodes --no-headers | awk -F ' ' '{print $1}' | head -n1 | xargs -l1 -- sh -c 'kubectl uncordon "$1"' -- – AhmFM Dec 18 '18 at 01:05
  • i know, am so happy. i have a follow up question: how can i pick a range? say rows 11-20? coz am done with first 10. – AhmFM Dec 18 '18 at 01:09
  • @AhmFM You could do `awk 'NR >=11 && NR <=20'`, or replace `head` (personally, I would drop the `grep` and the `head` and do it all in awk) with `sed -n -e 11,20p` – William Pursell Dec 18 '18 at 01:35
  • More options for this can be found here: https://stackoverflow.com/questions/83329/how-can-i-extract-a-predetermined-range-of-lines-from-a-text-file-on-unix – steve Dec 18 '18 at 01:42
  • ok.thx. also, could anyone please explain me the parameters that xargs is taking?> what each means? I know $1 is referring to my one column – AhmFM Dec 18 '18 at 01:58