Matching - Quick Perl Question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matching - Quick Perl Question
# 1  
Old 06-12-2013
Matching - Quick Perl Question

Hi everyone

I have a quick perl matching question. I have the following file, and I want to use perl to search through the 2nd column and see if it finds any of the month names (e.g.: Jan, Feb, Mar, ... Dec).

Here's the file I'm trying to search, and here's the code I have so far. Any help would be super helpful! Thanks Smilie
Code:
lu Aug 2006 -122.24 48.76
lu AuG 2006 -122.24 48.76
21 2 2006 -122.24 48.76
2A Jul 2008 -117.8617 34.7017
21 Ma2 2006 -112.24 48.76

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

use strict;
use warnings;

my $file = 'sample1.xyt';
open my $info, $file or die "Could not open $file: $!";


        my $x1 = $_;
        my @cols = split(" ", $x1);



	#Test: to see if col2 has values that match the following EXACTLY: Jan, Feb, Mar, Apr, Jun, Jul, Aug, Sep, Oct, Nov, Dec AND ALSO check that there are no numbers in this column. If these criteria is not met, print the bad line

        if ($cols[1] =~ /^[0-9]+$/) {   #checks for all numbers in the month
                print "I have a bad month here - $x1";
        
        } elsif ($cols[1] =~ /^[a-zA-Z]+$/) {   #checks for all letters

                #check in here for matches!
                @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); 

                ##Not sure what to do here! :-(


                } else {
                        print "I have a bad month here - $x1";

                } 

        

        } else {
                print "I have a bad month here - $x1";

        }


}
close $info;


Last edited by Scott; 06-12-2013 at 08:15 PM.. Reason: Please use CODE tags not ICODE tags
# 2  
Old 06-12-2013
Try something like this which uses the "smart match" to perform a "in list":
Code:
use strict;
use warnings;

my @months =  ( "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" );
my $file   =  'sample1.xyt';


open my $info, $file or die "Could not open $file: $!";
while ( <$info> ) {
  chomp;
  my @cols   =  split(" ");
  if ( $cols[1] ~~ @months ) {
    print "Valid month found: $cols[1]\n";
  } else {
    print "Invalid month found: $cols[1]\n";
  }
}

arrays - Perl: if ( element in list ) - Stack Overflow

Last edited by Scott; 06-12-2013 at 08:29 PM.. Reason: Removed FONT tag AGAIN
# 3  
Old 06-12-2013
How about this:

Code:
#!/usr/bin/perl -w
use strict;
use warnings;

my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
open FILE, "<sample1.xyt" or die "Could not open sample1.xyt";

foreach my $line (<FILE>) {
        my @cols = split(" ", $line);

        if ($cols[1] =~ /^[0-9]+$/) {   #checks for all numbers in the month
                print "I have a bad month (numbers) here - $cols[1]\n";
        } elsif (grep /^$cols[1]$/i, @months) {
                   print "Month is ok - $cols[1]\n";
        } else {
                print "I have a bad month here - $cols[1]\n";
        }
}
close(FILE);

# 4  
Old 06-12-2013
Alternation is probably faster than array comparison.
Code:
#!/usr/bin/perl

use strict;
use warnings;

open (my $file, '<', 'sample1.xyt');
while (<$file>){
   @rec=split/\s+/,$_;
   if ($rec[1]=~/^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/){
      ...
   }
else{
      print "Dud month in record $.:\n\t$_";
}

# 5  
Old 06-13-2013
even more simpler with regexp, no array
Code:
#!/usr/bin/perl

use strict;
use warnings;

open (my $file, '<', 'sample1.xyt');
while (<$file>){
   if ($_=~/.+\s+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+.+/i){
    print "month found $1\n";
   }
else{
      print "Dud month in record $.:\t$_";
}
}
close $file;

Code:
$ ./test.pl 
month found Aug
month found AuG
Dud month in record 3:    21 2 2006 -122.24 48.76
month found Jul
Dud month in record 5:    21 Ma2 2006 -112.24 48.76

# 6  
Old 06-14-2013
this was super useful and incredibly instructive... thanks guys! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Perl Pattern Matching Question

Hi all, I have a pattern matching problem in which i'm not sure how to attack. Here is my problem: I have a list of strings that appear in the following format: String: LE_(1234 ABC)^2^ABC^DEFG What i need to do is replace all the characters after the first ^ with blank. So the output... (2 Replies)
Discussion started by: WongSifu
2 Replies

2. UNIX for Dummies Questions & Answers

Quick question

Hi guys Quick question Im creating an FTP server and im chrooting each user to there home directory blah blah. Ive also setup scponly so there locked etc. Im a novice at unix and have just reaslised the primary group of scponly is the username of one of the ftp users... which im sure... (1 Reply)
Discussion started by: mokachoka
1 Replies

3. Shell Programming and Scripting

PERL - another quick hash of hashes question

Hi, sorry, two hash related questions in one day .. but this has got me a bit stuck. I have a mysql database table that kind of looks like this, the table is called "view1" and a snippet of that table (SELECT'ing just rows with serial number 0629AN1200) is below serial nic_name ... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

4. Shell Programming and Scripting

Quick question

When I have a file like this: 0084AF aj-123-a NAME Ajay NAME Kumar Engineer 015ED6 ck-345-c 020B25 ef-456-e 027458 pq-890-p NAME Peter NAME Salob Doctor 0318F0 xy-123-x NAME Xavier Arul NAME Yesu Supervisor 0344CA de-456-d where - The first NAME is followed by... (6 Replies)
Discussion started by: ajay41aj
6 Replies

5. Shell Programming and Scripting

quick question

I am using sed to find a pattern in a line and then I want to retain the pattern + the rest of the line. How is this possible? ie: line is: 14158 05-15-08 20:00 123-1234-A21/deliverable/dhm.a search for 123-1234-A21 ie: echo $line | sed 's/.*\(\{3\}-\{4\}-\{3\}\{5\}\).*/\1/' ... (1 Reply)
Discussion started by: phreezr
1 Replies

6. Shell Programming and Scripting

[B]Perl sort quick question[/B]

I've done a quick Google about this, but could not find the answer I want. Say, there is an array like this: @A = qw(cd1 a1 ef a2 hij a12 b2 b4 b22); I want to sort the array in the first order: @sorted = qw(a1 a12 a2 b2 b22 b4 cd1 ef hij); And second order: @sorted = qw(a1 a2 a12 b2 b4... (5 Replies)
Discussion started by: zx1106
5 Replies

7. UNIX for Dummies Questions & Answers

quick question

from command prompt I did grep two words on a same line for eg: grep abc | grep xyz and I got tht particular line, but I want to know when I vi that file how to directly search for that particular line? I appreciate if any one can provide answer, thanks in advance (2 Replies)
Discussion started by: pkolishetty
2 Replies

8. Shell Programming and Scripting

pattern matching + perl question

i can only find the first occurance of a pattern how do i set it to loop untill all occurances have changed. #! /usr/bin/perl use POSIX; open (DFH_FILE, "./dfh") or die "Can not read file ($!)"; foreach (<DFH_FILE>) { if ($_ !~ /^#|^$/) { chomp; ... (1 Reply)
Discussion started by: Optimus_P
1 Replies

9. Shell Programming and Scripting

Quick perl question

Hey everyone, I am a newbie with perl, and I just have a quick question that may seem really stupid. Like I said, I'm new, so please bear w/ me :) I'm trying to make a really simple program where there are two inputs. First one is just a string, and the next one is a number. Once the user... (7 Replies)
Discussion started by: jason_v
7 Replies

10. Shell Programming and Scripting

A very quick question

Just a super quick question: how do you put a link in your php code. I want to make a link to something in /tmp directory. i.e. how do you put a href into php, I think it's done a bit differently. thanks john (1 Reply)
Discussion started by: jmg5
1 Replies
Login or Register to Ask a Question