LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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
 
LinkBack Search this Thread
Old 01-10-2006, 12:45 PM   #1
TheMeteorPolice
LQ Newbie
 
Registered: Oct 2005
Location: USA
Distribution: Fedora Core
Posts: 5

Rep: Reputation: 0
Regex Question: Only print part of line that matches


Greetings,

Overview: I need to be able to use a regular expression to display only parts of a line that match the expression.

Input: A file that is filled with error messages from different hosts. Each line has an error message and the IP address that caused the error.

Desired Output: A list of all the IP addresses found in the file.


Here is what I have currently. It returns the proper lines, but I only want to print the part of the line that matches, so I end up with a list of IP addresses only. The IP address could be located anywhere in the line so I cant use something like awk to just print the proper field - it will be different for each returned line.

Code:
egrep '\([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\)'
Thanks in advance guys
 
Old 01-10-2006, 01:20 PM   #2
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 30
Here is a perl script I wrote a while back. It actually prints the ips and how many times they appear, but you should be able to alter it to suit your needs. You can put your input file(s) as an argument or feed whatever input to stdin.

Code:
#!/usr/bin/perl

use warnings;
use strict;
use Getopt::Long;


my $thresh = 0;
my $comma;
my %ips;

GetOptions(
           "threshold=i" => \$thresh,
           "comma" => \$comma,
           ) or die "Options problem";

while (<>) {
      if (m[((\d{1,3}\.){3}\d{1,3})]) {
        $ips{$1}++;
      }
}

foreach my $ip (keys %ips) {
    next unless $ips{$ip} > $thresh;
    if ($comma) {
        print ",$ip";
    } else {
        printf "%-18s - %5d\n", $ip,$ips{$ip};
    }
}
 
Old 01-10-2006, 03:48 PM   #3
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-19-live-usb/ aix
Posts: 3,551

Rep: Reputation: 513Reputation: 513Reputation: 513Reputation: 513Reputation: 513Reputation: 513
Quote:
Originally Posted by TheMeteorPolice
Greetings,

Overview: I need to be able to use a regular expression to display only parts of a line that match the expression.

...

Thanks in advance guys
the grep manpage says option -o only prints the part matched by pattern.
 
Old 01-11-2006, 12:41 PM   #4
TheMeteorPolice
LQ Newbie
 
Registered: Oct 2005
Location: USA
Distribution: Fedora Core
Posts: 5

Original Poster
Rep: Reputation: 0
Thumbs up

Ah yes you are correct. I was working on a Solaris box that did not have the -o option, but after moving the data to my Fedora workstation I have it.

Thanks!!
 
Old 01-12-2006, 04:52 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,269

Rep: Reputation: 165Reputation: 165
Quote:
I was working on a Solaris box that did not have the -o option,
I had exactly the same problem

I ended up with this:

Code:
#!/usr/bin/perl -wn


print "$ARGV:$&\n" while  ( m/YOUR_PATTERN_HERE/g ) ;
works nicely
 
Old 01-12-2006, 01:21 PM   #6
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-19-live-usb/ aix
Posts: 3,551

Rep: Reputation: 513Reputation: 513Reputation: 513Reputation: 513Reputation: 513Reputation: 513
i have problems like this all the time between aix(work) and linux(home).

very annoying to say the least.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sed to extract multiple matches in a line? mhoch3 Linux - Software 8 08-01-2005 03:32 PM
bash: routine outputting both matches and non-matches separately??? Bebo Programming 8 07-19-2004 06:52 AM
PERL editing single line in file with regex indiescene Programming 0 04-14-2004 08:18 AM
sed - multiple matches on the same line mjoc27x Programming 6 04-17-2003 07:22 AM
help me match this regex line (easy) JustinHoMi Programming 7 03-17-2002 01:43 AM


All times are GMT -5. The time now is 10:48 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration