
BillKSmith
Veteran
Feb 23, 2012, 9:22 PM
Post #4 of 6
(1098 views)
|
|
Re: [jeffersno1] regular expressions aaarrrrrhhh
[In reply to]
|
Can't Post
|
|
Your last attempt is very close. You need .* to match the characters in between the ones you want. Note: You need the options /ms to force the .* to match newlines (Refer: perldoc perlop). A bigger problem is that an input record consists of four "lines". Setting the $INPUT_RECORD_SEPARATOR (refer: perldoc perlvar) to a null string tells perl to treat blank lines, rather than newlines, as record separators. Now, each read reads a full logical record into the string $line.
#!/usr/bin/perl use strict; use warnings; use English; my $fup1 = '/temp/users'; open my $INFILE, '<', $fup1 || die "cannot open $fup1 $!\n" ; $INPUT_RECORD_SEPARATOR = q(); while ( my $line = <$INFILE> ) { chomp $line; my ($fup, $value) = $line =~ m/VAL=(\d+).+<\s(\d+)\s>/xms; print "$fup = $value\n"; } close($INFILE); Your script would 'work' without the following changes, but good practice demands them. Always use strict and use warnings. Use lexical (my) variables for file handles. Use the three argument form of open. Good Luck, Bill
|