I have a input file with the following example data.
kernel_version hostname
2.6.32-220.el6.x86_64 www01.dc1.domain.com
2.6.32-220.el6.x86_64 www02.dc1.domain.com
2.6.32-220.el6.x86_64 exc01.dc1.domain.com
2.6.32-220.el6.x86_64 exc02.dc1.domain.com
2.6.32-120.el6.x86_64 www03.dc2.domain.com
2.6.32-120.el6.x86_64 www04.dc2.domain.com
2.6.32-120.el6.x86_64 exc03.dc2.domain.com
2.6.32-120.el6.x86_64 exc04.dc2.domain.com
2.6.32-100.el6.x86_64 www05.dc3.domain.com
2.6.32-100.el6.x86_64 www06.dc3.domain.com
2.6.32-100.el6.x86_64 exc05.dc3.domain.com
2.6.32-100.el6.x86_64 exc06.dc3.domain.com
2.6.32-220.el6.x86_64 www07.dc4.domain.com
2.6.32-220.el6.x86_64 www08.dc4.domain.com
2.6.32-220.el6.x86_64 exc07.dc4.domain.com
2.6.32-220.el6.x86_64 exc08.dc4.domain.com
I'd like to output the unique kernel version, and which dcs are running which.
For example;
2.6.32-220.el6.x86_64 dc1, dc4
2.6.32-120.el6.x86_64 dc2
2.6.32-100.el6.x86_64 dc3
What is the best method to achieve this?
I'd do something like this:
#!/usr/bin/perl
use warnings;
use strict;
my %kernels;
open INPUT,"<inputfile";
while (<INPUT>)
{
my ($version, $hostname) = split(/\s+/,$_);
if ($kernels{$version}) { $kernels{$version} .= ", $hostname" }
else { $kernels{$version} = $hostname }
}
close INPUT;
# Print the summary
foreach my $kernel (keys(%kernels)) { print "$kernel\t$kernels{$kernel}\n"; }
Warning: I did not test this script. I am not responsible for any syntax errors, race conditions, or velociraptor attacks that may be caused by this code.. but it should get you started
Edit: I have now tested it. One syntactical error that has been rectified, and no major velociraptor attacks.
This might work for you:
sed 's/ [^\.]*\.\([^.]*\).*/ \1/' file |
sort -u |
sed ':a;$!N;s/^\(\(.*\) .*\)\n.*\2/\1,/;ta;P;D'
This gives the result but not in original order, for original order use:
sed 's/ [^\.]*\.\([^.]*\).*/ \1/' file |
cat -n - |
sort -uk2,3 |
sed ':a;$!N;s/^\(.\{7\}\(.*\) .*\)\n.*\2/\1,/;ta;P;D' |
sort -n |
sed 's/^.\{7\}//'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With