Apply comma in awk on every element except the last one

9/25/2019

I want to get a list of all kubernetes nodes with seperated by comma for using it in NO_PROXY env variable and wrote the following commands:

kgnoowide | grep -v NAME | awk '{printf  "%s,", $1}'

This itself works, with a little detail missing: When having three nodes like

node1
node2
node3

my awk command returns node1,node2,node3,

So the last comma is wrong, a perfect output would be node1,node2,node3. I know this is a minor problem that could easily solved by simply removing the comma when pasting. But I'm interested how to solve this with awk, that itself is a very powerfull tool so I assume it should be possible. And it's also relevant for later usage in scripts, that e.g. generate a ~/.bashrc entry with the correct NO_PROXY variable.

-- Daniel
awk
kubernetes

1 Answer

9/25/2019

You don't need grep there

kgnoowide | awk '!/NAME/{out=(out sep $0);sep=","} END{print out}'
-- oguz ismail
Source: StackOverflow