Perl only matching single-character regex patterns?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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...
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.