The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
perl question convenientstore Shell Programming and Scripting 7 05-21-2007 08:59 AM
Calling a perl script from a perl script new2ss Shell Programming and Scripting 3 02-06-2007 07:17 PM
Perl: Run perl script in the current process vino Shell Programming and Scripting 10 12-09-2005 06:45 AM
Question about Perl SolidSnake Shell Programming and Scripting 5 06-10-2003 06:20 AM
PERL question frank Shell Programming and Scripting 1 06-18-2002 12:13 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-23-2008
Registered User
 

Join Date: Oct 2007
Posts: 25
Stumble this Post!
question on perl script

LOGFILE
========
My name is: ?Anthony Perkins
I am an American citizen.
My name is: ?Donte Suarez
I am a Spanish citizen.
My name is: ? lenny Davis
I am an Australian citizen.
My name is: ?allen rigodeau
I am a French citizen.
My name is: ? manuel williams
I am a Mexican citizen.

OUTPUT
=======
First Name - ANTHONY
Last Name - PERKINS
First Name - DONTE
Last Name - SUAREZ
First Name - LENNY
Last Name - DAVIS
First Name - ALLEN
Last Name - RIGODEAU
First Name - MANUEL
Last Name - WILLIAMS

>>>>>>>>>>

SCRIPT
=======

if ( $currline =~ m/My name is\: \?/ )
{
@record = split(/\?/, $currline);
$message = "$record[1]";
@record2 = split(/ /, $message);
$firstname= "$record2[0]";
$lastname = "$record2[1]";
print OUTPUT "First Name -".$firstname;
print OUTPUT "Last Name -".$lastname;
}

>>>>>>>>>>

For the above logfile, my script would be working properly only on Anthony Perkins and allen rigodeau.
But this no longer works if there's a space after the question mark, or when there's more than one space between the first and last name.

Appreciate your advice.
Thanks.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 07-23-2008
Registered User
 

Join Date: Jul 2008
Location: BlackMesh Managed Hosting
Posts: 61
Stumble this Post!
Check out split - perldoc.perl.org. You're splitting the array by spaces, which means if there's more than one space, your array's all off. Try this:

Code:
use Data::Dumper;

if ( $currline =~ m/My name is\: \?/ )
{
        @record = split(/\?/, $currline);
        $message = "$record[1]";
        @record2 = split(/ /, $message);
print Dumper(@record2);
        $firstname= "$record2[0]";
        $lastname = "$record2[1]";
        print OUTPUT "First Name -".$firstname;
        print OUTPUT "Last Name -".$lastname;
}
and you'll see the error.

Reading a bit more, and we can see that what we want is:

Code:
if ( $currline =~ m/My name is\: \?/ )
 {
         @record = split(/\?/, $currline);
         $message = "$record[1]";
         @record2 = split(' ', $message);
         $firstname= "$record2[0]";
         $lastname = "$record2[1]";
         print OUTPUT "First Name -".$firstname;
         print OUTPUT "Last Name -".$lastname;
 }
Alternately, try:
Code:
if ( $currline =~ m/My name is\: \?\s*(\S+)\s+(\S+)/ )
 {
         print OUTPUT "First Name -".$1;
         print OUTPUT "Last Name -".$2;
 }
Reply With Quote
  #3 (permalink)  
Old 07-25-2008
Registered User
 

Join Date: Jun 2007
Posts: 377
Stumble this Post!
Code:
cat file | sed -n '/?/p' | sed 's/?/ /' | awk '{ print "First Nmae-"$(NF-1);print "Last Name-"$NF}'
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:57 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0