LinuxQuestions.org
Review your favorite Linux distribution.
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 01-20-2009, 07:18 AM   #1
Lordandmaker
Member
 
Registered: Sep 2005
Location: London, UK
Distribution: Debian
Posts: 258

Rep: Reputation: 39
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

Last edited by Lordandmaker; 01-20-2009 at 07:24 AM.
 
Old 01-20-2009, 07:55 AM   #2
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
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.].

Last edited by Telemachos; 01-20-2009 at 07:58 AM.
 
Old 01-20-2009, 08:11 AM   #3
Lordandmaker
Member
 
Registered: Sep 2005
Location: London, UK
Distribution: Debian
Posts: 258

Original Poster
Rep: Reputation: 39
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...
 
Old 01-20-2009, 08:59 AM   #4
Lordandmaker
Member
 
Registered: Sep 2005
Location: London, UK
Distribution: Debian
Posts: 258

Original Poster
Rep: Reputation: 39
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...
 
  


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
any character matching doesn't yield expected result in PERL gaynut Programming 4 09-26-2008 01:04 AM
Matching two wildcards with perl and regex borinus Programming 3 09-09-2008 04:04 AM
Embedded regex matching in Perl GATTACA Programming 5 01-17-2007 09:16 AM
perl regex matching exodist Programming 2 11-15-2004 10:50 PM
PERL editing single line in file with regex indiescene Programming 0 04-14-2004 08:18 AM

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

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