LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 11-02-2011, 07:47 PM   #1
haux
LQ Newbie
 
Registered: Jan 2010
Location: canada
Distribution: Ubuntu, Redhat, Debian
Posts: 9

Rep: Reputation: 0
Select a text between 2 strings (Perl)


I wanna select just two values from our web server's log.

The log looks like this :


100.200.200.100 - - [15/Mar/2011:12:01:09 -0400] "GET /mediaencoder.php?logins=MYMP3;J5J5J5;0%7CCFWMFM;Q5Q5Q5;2%C8;1&version=1.1&meversion=1.3.0 HTTP/1.1" 200 8751 "-" "-" 163 8912

I want to select the words between "logins=" and ";" (MYMP3 in the exemple line). I tried this :


$file="/files.txt";
open(SESAME, $file);
while(<SESAME>)
{
my($line) = $_;

if ($line =~ /\blogins=\b(.*?)\b';'\b/) {
$result = $1;
# do something with results
print "RESULT= $result\n";
}
}

close (SESAME);



But I get an empty value for $RESULT. Do you have any idea why it doesn' t work ?
 
Old 11-02-2011, 08:18 PM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
You want to match a word in the example, so need only 2 \b, one before and one after the word
Code:
if (/logins=\b(.*?);\b/) {
This also works :
Code:
if (/logins=(.*?);/)
 
1 members found this post helpful.
Old 11-03-2011, 07:58 AM   #3
haux
LQ Newbie
 
Registered: Jan 2010
Location: canada
Distribution: Ubuntu, Redhat, Debian
Posts: 9

Original Poster
Rep: Reputation: 0
Great! thank you Cedrik it works :-)

I want also to select the IP (the first column in each line) I am trying this but it doesn't work :

if ($line =~ /^\b(\d+\.\d+\.\d+\.\d+)\b\s/) {
$IP_SOURCE = $1;
print "RESULT= $result $IPSOURCE\n";
}




I don't understand where is the problem !

Thx
 
Old 11-03-2011, 08:19 AM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Keep it simple...
Code:
if(/^(\d+\.\d+\.\d+\.\d+)/) {
[edit]
To grep both IP and logins values, you may try:

Code:
while(<SESAME>)
    my ($ip, $login) = (/^(\d+\.\d+\.\d+\.\d+).+logins=(.*?);.*/);
    print "IP: $ip LOGIN: $login\n";
}

Last edited by Cedrik; 11-03-2011 at 08:36 AM.
 
1 members found this post helpful.
Old 11-03-2011, 08:58 AM   #5
haux
LQ Newbie
 
Registered: Jan 2010
Location: canada
Distribution: Ubuntu, Redhat, Debian
Posts: 9

Original Poster
Rep: Reputation: 0
Great thank you !
 
  


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
keep text between two strings jimmyrp Linux - Newbie 1 05-16-2011 10:47 PM
How to store text(strings) in a 2D character array reading from a text file(C++) bewidankit Programming 3 02-14-2008 07:08 AM
using variables as strings in perl Elukyz Programming 9 06-17-2007 12:22 AM
Perl: Ord() with strings KneeLess Programming 1 09-05-2004 03:59 PM
Perl>>working with strings format_c Programming 2 11-15-2001 04:21 PM

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

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