Aggregate variables bdfore ssh into remote host

Hi all,

I have a problem where i'm trying to identify on which remote hosts the apps are running, ssh into it and restart them.

In case more than 1 apps is running on same remote host, i want to be able to group it and ssh only once.

E.g:

app1 = 1.1.1.1
app2 = 1.1.1.2
app3 = 1.1.1.1
app4 = 1.1.1.3
 

Any thoughts?

I have played with the arrays but couldn't figure out.

Thank you for your help.

---------- Post updated 02-17-14 at 09:39 AM ---------- Previous update was 02-16-14 at 11:56 PM ----------

the complexity is in that i don't want to do multiple if else for each permutations

app1 = app2
app1 = app3
app1 = app4
app2 = app3
app2 = app4
app3 = app4

Hope is not too hard to achieve it.

Not sure if i understood correctly, but this may helps:

# awk  '{a[$NF]=sprintf("%s%s ",a[$NF],$1)}END{for (i in a){print i,a}}' infile
1.1.1.1 app1 app3 
1.1.1.2 app2 
1.1.1.3 app4
1 Like

Thanks a lot!

any chance you can explain the line, mainly the first part before the END?

---------- Post updated at 11:55 AM ---------- Previous update was at 11:43 AM ----------

No need as i figured out,

thanks again !!

---------- Post updated at 05:42 PM ---------- Previous update was at 11:55 AM ----------

One more question please ...

for the above code, is it possible to write it to unique file based on the first argument - in our case i ?

e.g

1.1.1.1 app1 app3 > 1.1.1.1_txt
1.1.1.2 app2 > 1.1.1.2_txt
1.1.1.3 app4 > 1.1.1.3_txt

Thx

awk '
        {
                A[$NF] = A[$NF] ? A[$NF] FS $1 : $1
        }
        END {
                for ( k in A )
                        print k, A[k] > k "_txt"
        }
' file

If you want to use the same script:

awk  '{a[$NF]=sprintf("%s%s ",a[$NF],$1)}END{for (i in a){print i,a > i"_txt"} }' infile