0

When I run the following command, I expect to get the ip addresses of ASG member instances:

current_servers=$(aws ec2 describe-instances --instance-ids $(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names "${asgname}" --region $region | grep InstanceId | awk -F: '{print $2}' | tr -d '\"|,' | tr -d '\n') --region $region | grep "PrivateIpAddress" | grep -v '\[' | awk -F: '{print $2}' | uniq -u | tr -d '\"|,' )

For some reason, the I get no output.

But if I change the uniq -u command to just uniq, then I get the correct output.

From uniq's man:

-u, --unique
              only print unique lines

Without the uniq command, the output is:

 172.51.39.73
 172.51.39.73
 172.51.39.73

So it seems (by uniq's man) that if I want to get only one occurrence of the output I have to use uniq -u.

Anyone knows why the command acts like that? that is giving me no output if I use the "-u" switch?

Itai Ganot
  • 10,644
  • 29
  • 93
  • 146

1 Answers1

1

That's not what uniq does. Without the -u switch, it combines multiple subsequent (!) identical lines into one line, and with the -u switch it omits these lines (as they are not uniqe):

sven@linux:~$ cat uniq.txt
Line1            ## these two lines are not unique. 
Line1
Line2
Line1
sven@linux:~$ uniq uniq.txt
Line1
Line2
Line1
sven@linux:~$ uniq -u uniq.txt
Line2
Line1

So, in your case, since your IP address is repeated two times, it will not be shown with the -u switch and this is normal (but not very intuitive, as it is often the case with these kind of tools).

Sven
  • 98,649
  • 14
  • 180
  • 226