LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl only matching single-character regex patterns? (https://www.linuxquestions.org/questions/programming-9/perl-only-matching-single-character-regex-patterns-698580/)

Lordandmaker 01-20-2009 07:18 AM

Perl only matching single-character regex patterns?
 
I'm having a small issue with regex matching in Perl. I'm pretty certain it's a simple fix, but it all looks correct to me...

Any ideas?

If I run the following:
Code:

#! /usr/bin/perl

my $input_file = "./report_svr09.txt";
#print $input_file;

open FILE, "$input_file" || die ("Error opening input file at $input_file");
        while (<FILE>){
                $line = $_;
                if ($line =~ m/P/){
                        print $line;
                }
        }
close FILE;

print "\n";

It prints out all the lines containing a 'P', as one would expect. But when the regex is

Code:

                if ($line =~ m/Pro/){
I get zero lines printed. It seems to match only single-character patterns.

The file I'm reading is:
(It has the same effect whether I leave it with Windows linebreaks or convert them to unix).
Code:

??System Information report written at: 01/20/09 09:24:45
System Name: JUP-SVR09
[System Summary]

Item    Value
OS Name Microsoft(R) Windows(R) Server 2003, Standard Edition
Version 5.2.3790 Service Pack 2 Build 3790
Other OS Description    Not Available
OS Manufacturer Microsoft Corporation
System Name    JUP-SVR09
System Manufacturer    Dell Computer Corporation
System Model    PowerEdge 1800
System Type    X86-based PC
Processor      x86 Family 15 Model 4 Stepping 3 GenuineIntel ~3192 Mhz
Processor      x86 Family 15 Model 4 Stepping 3 GenuineIntel ~3192 Mhz
BIOS Version/Date      Dell Computer Corporation A07, 29/09/2006
SMBIOS Version  2.3
Windows Directory      C:\WINDOWS
System Directory        C:\WINDOWS\system32
Boot Device    \Device\HarddiskVolume2
Locale  United Kingdom
Hardware Abstraction Layer      Version = "5.2.3790.3959 (srv03_sp2_rtm.070216-1710)"
User Name      Not Available
Time Zone      GMT Standard Time
Total Physical Memory  2,047.27 MB
Available Physical Memory      314.45 MB
Total Virtual Memory    7.85 GB
Available Virtual Memory        6.31 GB
Page File Space 6.00 GB
Page File      E:\pagefile.sys

[Components]



[Storage]



[Drives]

Item    Value
Drive  C:
Description    Local Fixed Disk
Compressed      No
File System    NTFS
Size    12.00 GB (12,880,787,968 bytes)
Free Space      4.42 GB (4,745,418,752 bytes)
Volume Name
Volume Serial Number    A4B40FDF

Drive  D:
Description    CD-ROM Disc

Drive  E:
Description    Local Fixed Disk
Compressed      No
File System    NTFS
Size    7.00 GB (7,517,904,896 bytes)
Free Space      3.29 GB (3,534,249,984 bytes)
Volume Name    SYS
Volume Serial Number    BE1F14B0

Drive  F:
Description    Local Fixed Disk
Compressed      No
File System    NTFS
Size    254.21 GB (272,955,916,288 bytes)
Free Space      182.64 GB (196,106,596,352 bytes)
Volume Name    DATA
Volume Serial Number    9448FDDF

Drive  J:
Description    Network Connection
Provider Name  \\jup-svr01\jbwv2

Drive  M:
Description    Network Connection
Provider Name  \\jup-svr02\macshare

Drive  S:
Description    Network Connection
Provider Name  \\jup-svr01\Shared

Drive  U:
Description    Network Connection
Provider Name  \\jup-svr01\users\Avi Greenbury


Telemachos 01-20-2009 07:55 AM

It works fine here. Output:
Code:

telemachus ~ $ ./testing
Processor      x86 Family 15 Model 4 Stepping 3 GenuineIntel ~3192 Mhz
Processor      x86 Family 15 Model 4 Stepping 3 GenuineIntel ~3192 Mhz
Provider Name  \\jup-svr01\jbwv2
Provider Name  \\jup-svr02\macshare
Provider Name  \\jup-svr01\Shared
Provider Name  \\jup-svr01\users\Avi Greenbury

There may be a typo at your end or something else going on that I can't see. For whatever it's worth, you can also write this a lot more simply:
Code:

#!/usr/bin/env perl
use strict;
use warnings;

while (<>) {
    print if m/Pro/;
}

print "\n";

Both print and the regex match will operate by default on $_ which is exactly what each line of the file is aliased to in that while loop. Then you just run it with the name of the file or files you want to check on the command line, eg ./scriptname filename [filename2 filename3 etc.].

Lordandmaker 01-20-2009 08:11 AM

Yeah, I know that bit could be a lot more simple, but it's to be a middle bit in a longer process - I tend to start with the bit that would require the most work, then add the simpler bits (looping through the files, formatting the output) later on. It's going to push the variable if it matches in the end.

But, if it works for you, I'll get cracking on working out what's wrong with mine...

Lordandmaker 01-20-2009 08:59 AM

Just spotted the '??' at the beginning of the file, and the fact it worked for you so copied and pasted the input file into a new one, and it all seems to work.
So I try to grep the first line off it, and get the following:

Quote:

avi@jup-linux2:~$ cat report_svr09 | grep -v "report written at"
Binary file (standard input) matches
avi@jup-linux2:~$
This was produces from Microsoft's MSinfo32.exe, which is typically useless on info as regards its output...

Looks like this problem doesn't belong in a programming forum...


All times are GMT -5. The time now is 05:35 PM.