Question

I have a long .csv file that consists of device information. Each line is a unique device with its own info listed as such:

MACaddr,,,,Date,Date,,"b,n",,blahblahblah

What I need to do is take in the file and then write to a new file with all lines/devices that have the same Vendor MAC (i.e. the first 3 octets of the MAC Address) grouped together. I can easily take in each line and grab the Vendor MAC with a regex but then I am stuck.

All I have is:

#!usr/bin/perl
use strict;
use warnings;

open IN, "wirelessClients.csv" or die "Couldn't open file\n$!";
open OUT, ">sortedClients.csv" or die "Couldn't open out file\n$!";

my @clients = <IN>;

foreach my $client (@clients)
{
    if($client =~ /^(\w+:\w+:\w+)/)
    {
        print OUT "$1,$client\n\n"; 
    }
}

I have no idea how to go about sorting the information.

Any help and/or guidance would be greatly appreciated!

Was it helpful?

Solution

If the MAC address is the first element, a simple sort should group ones with the same first 3 octets:

sort wirelessClients.csv > sortedClients.csv

OTHER TIPS

As we're looking at the first octets anyway you can simply sort numerically alphanumerically:

#!/usr/bin/perl

use strict;
use warnings;


open (my $in, '<', "wirelessClients.csv") or die "Cannot open Infile: $!";
open (my $out, '>',"sortedClients.csv") or die "Cannot open Outfile: $!";

my @clients = <$in>;

my @sorted = sort {$a cmp $b} @clients;

foreach @sorted {
    print $out $_;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top