LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-07-2012, 01:53 AM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
perl print regex string


SO I have this file of mac addresses . only the last six digit pairs are the mac address.

Code:
        312.47B 31      00 15 17 81 4D 94
        312.47B 32      00 15 17 9B AF AC
        312.47A 14      00 15 17 64 0C 6C
        312.47A 15      00 15 17 81 DD D4
        312.48B 33      00 15 17 99 97 10
        312.48B 34      00 15 17 81 A3 F0
        312.48B 35      00 15 17 C1 02 A0
and I have this code:
Code:
#!/usr/bin/perl

open FILE, "/tmp/foo" or die $!;
while (<FILE>) {
$string = $_ ;
chomp $string;
if ($string =~ /\d{2}\s+(\d{2}\s\d{2}\s\d{2}\s\d{2}\s\d{2}\s\d{2})/) {
        print $string;
}

sleep 1;
}
and this is all it prints:

312.48B 33 00 15 17 99 97 10

what I want to print is the last 6 digits of each line - space seperated - theyare actuallly mac addresses. Eventually, I want to use the TR values to replace the spcaes with colons.
but first I have to print them out.

Last edited by casperdaghost; 06-07-2012 at 02:03 AM.
 
Old 06-07-2012, 01:55 AM   #2
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
even when i change the regex to this - still same thing.
Code:
/(00\s\d{2}\s\d{2}\s\d{2}\s\d{2}/
 
Old 06-07-2012, 02:12 AM   #3
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
I am not a perl expert, but what did you expect?
You do realize that the values are hexadecimal and the script only outputs the one line that only contains digits, right?
 
Old 06-07-2012, 02:17 AM   #4
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
oh crap sorry
 
Old 06-07-2012, 05:04 AM   #5
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Also, the code will print the entire line if it contains the pattern. To print only the matched part, something like this might work

Code:
	if ($string =~ s/.*(([0-9A-F]{2}\s*){6})$/\1/) {
        	print "$string\n";
	}
 
1 members found this post helpful.
Old 06-07-2012, 08:41 AM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by casperdaghost View Post
SO I have this file of mac addresses . only the last six digit pairs are the mac address.

Code:
        312.47B 31      00 15 17 81 4D 94
        312.47B 32      00 15 17 9B AF AC
        312.47A 14      00 15 17 64 0C 6C
        312.47A 15      00 15 17 81 DD D4
        312.48B 33      00 15 17 99 97 10
        312.48B 34      00 15 17 81 A3 F0
        312.48B 35      00 15 17 C1 02 A0
and I have this code:
Code:
#!/usr/bin/perl

open FILE, "/tmp/foo" or die $!;
while (<FILE>) {
$string = $_ ;
chomp $string;
if ($string =~ /\d{2}\s+(\d{2}\s\d{2}\s\d{2}\s\d{2}\s\d{2}\s\d{2})/) {
        print $string;
}

sleep 1;
}
and this is all it prints:

312.48B 33 00 15 17 99 97 10

what I want to print is the last 6 digits of each line - space seperated - theyare actuallly mac addresses. Eventually, I want to use the TR values to replace the spcaes with colons.
but first I have to print them out.
How about using web search, e.g. https://duckduckgo.com/?q=perl+regul...on+mac+address , and reading the first match ?
 
1 members found this post helpful.
Old 06-17-2012, 11:40 PM   #7
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
thanks guys - I did nt initially realize that the mac were in hex - not just dec.
working late .
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM
Perl to find regex and print following 5 lines after regex casperdaghost Linux - Newbie 3 08-29-2010 08:08 PM
[SOLVED] How to include a string variable in a regex (Perl) MTK358 Programming 5 01-09-2010 11:31 AM
Sed and regex: how to replace up to a certain string olliesa Linux - General 2 10-21-2009 02:49 AM
How to PRINT a range of letters of a string in Perl resetreset Programming 1 01-27-2009 08:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration