Uniq entries needed... (108 Views)
Reply
Super Advisor
Allanm
Posts: 287
Registered: ‎08-13-2007
Message 1 of 3 (108 Views)

Uniq entries needed...

Hi All,

I have the following command which returns the output like this -

wget URL|grep hostname|awk '{print $1,$3}'|/usr/bin/perl -nle 'm{"(.*?)".*?//(.*):(\d+)/} and print "$1 $2 $3"'|while read host port
do
echo check_env_$port
echo host
done

OUTPUT
Appname hostname port
Appname2 hostname port2 ...

Appnames are all unique , while hostname is not since I am grepping for single hostname(vip).
Some of the ports are not unique so I am getting the same port for 4-5 different appnames.

Within the same snippet of code I want to add a number to echo command so the its unique..
e.g. check_env_1_1234
check_env_2_1234

So for port 1234 it should increment the numbers.

If that's too hard then atleast get unique entries so that I don't get check_env_1234 not more than once which ever comes first and ignore the rest of the entries with the same port.

Thanks,
Allan.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 3 (108 Views)

Re: Uniq entries needed...

Hi Allan:

One solution is to sort your output before your 'while read...' loop. Then, in the body of the loop, compare and increment a variable whose value becomes the count of the port instance.

Regards!

...JRF...
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 3 (108 Views)

Re: Uniq entries needed...

Hi (again) Allan:

...and I scanned right past this:

> If that's too hard then atleast get unique entries so that I don't get check_env_1234 not more than once which ever comes first and ignore the rest of the entries with the same port.

OK, so, use a 'sort -u' :

# wget ... | sort -u -k3,3 | while read ...

Regards!

...JRF...
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.