LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-10-2008, 07:01 PM   #1
gliesian
LQ Newbie
 
Registered: Apr 2008
Posts: 18

Rep: Reputation: 0
Question Perl - Regular Expressions question...


I'm new to Perl and I'm trying to do the following with regular expressions.


For a list of IP addresses.. replace 'all' of the forth octets with astericks. Basically, I'm trying to substitute all digits after the third period with a single asterisk...

So
10.1.1.1
10.1.2.12
would end up like this
10.1.1.*
10.1.2.*

Can anyone help?

Thanks!
 
Old 04-10-2008, 07:14 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Code:
#!/usr/bin/perl

@ips = ("192.168.0.1", "127.0.0.1", "1.2.3.567");
my $replacement = "*";

foreach my $ip (@ips)
{
        print "original: $ip ; new: ";

        $ip =~ s/^(\d+\.\d+\.\d+\.)\d+$/${1}${replacement}/;
        print "$ip\n";
}
 
Old 04-10-2008, 07:19 PM   #3
gliesian
LQ Newbie
 
Registered: Apr 2008
Posts: 18

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by matthewg42 View Post
Code:
#!/usr/bin/perl

@ips = ("192.168.0.1", "127.0.0.1", "1.2.3.567");
my $replacement = "*";

foreach my $ip (@ips)
{
        print "original: $ip ; new: ";

        $ip =~ s/^(\d+\.\d+\.\d+\.)\d+$/${1}${replacement}/;
        print "$ip\n";
}
Yup! That works!

Thanks!
 
  


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
Perl regular expressions abdul_zu Programming 3 12-11-2007 03:21 AM
perl regular expressions arash8m Programming 3 07-13-2007 10:56 AM
Perl regular expressions CyberJedi Programming 2 10-02-2005 11:17 AM
Perl and Regular Expressions Fonk Programming 1 06-27-2004 11:32 AM
Perl Regular Expressions true_atlantis Programming 1 11-15-2003 05:16 PM

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

All times are GMT -5. The time now is 05:35 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