-2

Team, I read many posts but its unclear to me..

I need to understand how to deal with this.. I have xargs doing some evaluation and then am piping to awk but get syntax error

file.log

node1,
node2,
node3,

failing expression with xargs and awk

cat file.log | awk -F ',' '{print $1}' | xargs -l1 -- sh -c 'kubectl get pods --all-namespaces --no-headers --field-selector spec.nodeName=$1 | awk "{if ($1 ~ "team-") print $1,$2}"' --

passing till here

cat file.log | awk -F ',' '{print $1}' | xargs -l1 -- sh -c 'kubectl get pods --all-namespaces --no-headers --field-selector spec.nodeName=$1

as soon as i add awk part..am not getting it.

output:

awk: line 1: syntax error at or near )

Alternate solution I found is move the awk out of xargs --. so below works. but how can i get the awk inside because I would like to print the $1 that am using in xargs to see correlation with what awk is printing.

WORKS

--field-selector spec.nodeName=$1' -- | awk '{if ($1 ~ "team-") print $1,$2}'

FAILS

--field-selector spec.nodeName=$1 | awk '{if ($1 ~ "team-") print $1,$2}' --

AhmFM
  • 1,552
  • 3
  • 23
  • 53

1 Answers1

1

Figured it out with the help of "How do I use awk's single quotes within another single quote phrase?"

Here is what worked for me. I quoted awk in dQuotes and then escaped $ inside:

cat file.log | 
awk -F ',' '{print $1}' | 
xargs -l1 -- sh -c \
   'kubectl get pods --all-namespaces --no-headers \
                     --field-selector spec.nodeName=$1 | 
    awk "{if (\$1 ~ \"team-\") print \$1,\$2}"
    ' --
agc
  • 7,973
  • 2
  • 29
  • 50
AhmFM
  • 1,552
  • 3
  • 23
  • 53