×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

comparing 2 arrays for invalid mac addresses only
3

comparing 2 arrays for invalid mac addresses only

comparing 2 arrays for invalid mac addresses only

(OP)
I have 2 arrays and I want to compare and only print the non-valid macs in red/bold. The valid macs array will always be constant. I tried comparing 1 element at a time but doesn't work as it would still pick up valid array elements. Any help would be appreciated.

valid macs
90:40:95:29:C4:81
88:E9:10:20:49:8B
F0:F6:1C:9E:50:70

Log file with valid and invalid macs
1B:BB:1C:7D:34:77
88:E9:10:20:49:8B
F0:F6:1C:9E:50:70
F8:8A:2C:5F:FD:49
90:40:95:29:C4:81

RE: comparing 2 arrays for invalid mac addresses only

A perl 1-liner ;)

CODE

$ perl -e '%valid=(); open FH,$ARGV[0];while(<FH>){chomp;$valid{$_}=1;};close FH;open FH,$ARGV[1];while(<FH>){chomp;print "$_\n" if ( !defined $valid{$_} );};close FH;' 1st_file.txt 2nd_file.txt 
1B:BB:1C:7D:34:77
F8:8A:2C:5F:FD:49 

Or formatted for readability

CODE

$ perl -e '%valid=();
  open FH,$ARGV[0];
  while(<FH>){
    chomp;
    $valid{$_}=1;
  };
  close FH;
  open FH,$ARGV[1];
  while(<FH>){
    chomp;
    print "$_\n" if ( !defined $valid{$_} );
  };
  close FH;' 1st_file.txt 2nd_file.txt 
1B:BB:1C:7D:34:77
F8:8A:2C:5F:FD:49 

1st_file.txt is the valid list.
2nd_file.txt is the mixed list.

--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

RE: comparing 2 arrays for invalid mac addresses only

(OP)
Salem,

Wow, I appreciate this but I'm introducing folks bash scripts not perl. We'd like to be at level at some point. Can you provide a bash script version instead please? Thanks in advance.

RE: comparing 2 arrays for invalid mac addresses only

(OP)
I think this will do ..thanks for the help Salem!

var=$(comm -1 -3 <(sort ~/valid.txt) <(sort ~/invalid.txt))
for ((i = 0; i < ${#var[@]}; i++)); do
echo "${var[i]}" > ~/invalid_macs.txt
done

RE: comparing 2 arrays for invalid mac addresses only

Hi

My first thought on such task is usually grep :

CODE

grep -xvf valid-macs valid-and-invalid-macs > invalid-macs 

But I like your comm solution too. Somehow we never got close to each other and never noticed its -1 / -2 / -3 switches.

Feherke.
feherke.github.io

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close