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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-10-2006, 12:45 PM
|
#1
|
|
LQ Newbie
Registered: Oct 2005
Location: USA
Distribution: Fedora Core
Posts: 5
Rep:
|
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 
|
|
|
|
01-10-2006, 01:20 PM
|
#2
|
|
Member
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217
Rep:
|
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};
}
}
|
|
|
|
01-10-2006, 03:48 PM
|
#3
|
|
Senior Member
Registered: May 2005
Location: boston, usa
Distribution: fc-12/ fc-11-live-usb/ aix
Posts: 2,672
|
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.
|
|
|
|
01-11-2006, 12:41 PM
|
#4
|
|
LQ Newbie
Registered: Oct 2005
Location: USA
Distribution: Fedora Core
Posts: 5
Original Poster
Rep:
|
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!!
|
|
|
|
01-12-2006, 04:52 AM
|
#5
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
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
|
|
|
|
01-12-2006, 01:21 PM
|
#6
|
|
Senior Member
Registered: May 2005
Location: boston, usa
Distribution: fc-12/ fc-11-live-usb/ aix
Posts: 2,672
|
i have problems like this all the time between aix(work) and linux(home).
very annoying to say the least.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 02:42 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|