darthjef (TechnicalUser)
11 Sep 07 12:54
Perl regex questions for perl script using file::find
Hi Im having a problem getting my perl script to run properly. I'm new to perl and programming in general and am at a loss as to how to proceed.
I am trying to capture
a group variable ($1) from a regex (m/()/), save it to another variable ($date)
and then use the captured variable ($date) in a replace
of another regex (s/$1/g)
using file::find.
I have been trying to save the $1 to another variable and I don't know enough perl
to figure out what I am doing wrong.
I am looking at txt files of bibliographic records in MARC format.
Im trying to capture a date from a line and move it to another line in the record:
=099 \\$a336.2420971 CCH 2008 (CCH 2008 is the pattern I am looking for)
--match this pattern and then capture the date (2008)
m/\w\w\w\s(\d\d\d\d)/ -- I need to match this and then
capture the $1 as $date
...
--replace $o119$ywrqr with $o119$v2008$ywrq
(s/(\$o\d*)(\$yw\w\w*)/$1\$v$date$2/g)
=945 \1$h33420010942390$l120.00$o119$ywrqr
to
=945 \1$h33420010942390$l120.00$o119$v2008$ywrqr
my script is as follows:
#!/usr/bin/perl
use strict;
use diagnostics;
use File::Find;
my $startdir = 'c:/iw/WA/';
my $doctype = 'mrk';
my $regex = m/\w\w\w\s(\d\d\d\d)/; (don't think I am doing this correctly)
my $date = '';
$date = $1;
print qq~Finding 099 yyyy and removing it from 099...\n~;
find(
sub{
return unless (/\.$doctype$/i);
local @ARGV = $_;
local $^I = '.bac';
while( <> ){
if(s/(\$o\d*)(\$yw\w\w*)/$1\$v$date$2/g){
print;
}
else {
print;
}
}
}, $startdir);
When I run the script I get the following error messages:
Use of uninitialized value in pattern match (m//) at c:\PERL\BIN\wattst5.pl
line 9 (#1)
(W uninitialized) An undefined value was used as if it were already
defined. It was interpreted as a "" or a 0, but maybe it was a mistake.
To suppress this warning assign a defined value to your variables.
To help you figure out what was undefined, perl tells you what operation
you used the undefined value in. Note, however, that perl optimizes your
program and the operation displayed in the warning may not necessarily
appear literally in your program. For example, "that $foo" is
usually optimized into "that " . $foo, and the warning will refer to
the concatenation (.) operator, even though there is no . in your
program.
Finding 099 yyyy and removing it from 099...
Im trying to capture the $1 of my m// in $regex as $date but I obviously don't
know enough about perl to get this to work. If I was doing a s/// on the same line of text
I would be fine, but because I am trying to do a match on one line, capture a group variable,
then move that value to another line in a replace s/// I am at a loss as to how to get that to work.
Included is a sample record to process:
=LDR 01103cas 2200313 a 4500
=001 cn\75033109\
=005 20070829142812.0
=008 760206c19669999oncur4p\\\\\\\0\\\a0eng\d
=010 \\$acn 75033109
=020 \\$a9781553677604 (pbk. : 84th ed. : Summer : 2008) :$c{dollar}120.00
=020 \\$a9781553677437 (pbk. : 83rd ed. : Spring : 2007)
=022 \\$a0317-9060
=040 \\$aCaOONL$beng$cCaOONL$dm.c.$dNST$dlbi$dlbi
=042 \\$anlc$aisds/c
=043 \\$an-cn---
=082 04$a343/.71/052$222
=099 \\$a336.2420971 CCH 2008
=110 2\$aCanada.
=222 \0$aCanadian income tax act with income tax regulations
=245 10$aCanadian income tax act with regulations.
=260 \\$aDon Mills, Ont.,$bCCH Canadian,$c
=300 \\$av. ;$c23 cm.
=362 0\$a35th ed.(1966)-
=500 \\$aDescription based on current issue.
=650 \0$aIncome tax$xLaw and legislation$zCanada.
=650 \0$aIncome tax$zCanada$xLaw.
=710 2\$aC C H Canadian Limited.
=780 10$aCanada. Laws, Statutes, etc.$t[Income tax act] Canadian income tax act.
=945 \1$h33420010942390$l120.00$o119$ywrqr
Any help would be greatly appreciated.
11 Sep 07 12:54
Perl regex questions for perl script using file::find
Hi Im having a problem getting my perl script to run properly. I'm new to perl and programming in general and am at a loss as to how to proceed.
I am trying to capture
a group variable ($1) from a regex (m/()/), save it to another variable ($date)
and then use the captured variable ($date) in a replace
of another regex (s/$1/g)
using file::find.
I have been trying to save the $1 to another variable and I don't know enough perl
to figure out what I am doing wrong.
I am looking at txt files of bibliographic records in MARC format.
Im trying to capture a date from a line and move it to another line in the record:
=099 \\$a336.2420971 CCH 2008 (CCH 2008 is the pattern I am looking for)
--match this pattern and then capture the date (2008)
m/\w\w\w\s(\d\d\d\d)/ -- I need to match this and then
capture the $1 as $date
...
--replace $o119$ywrqr with $o119$v2008$ywrq
(s/(\$o\d*)(\$yw\w\w*)/$1\$v$date$2/g)
=945 \1$h33420010942390$l120.00$o119$ywrqr
to
=945 \1$h33420010942390$l120.00$o119$v2008$ywrqr
my script is as follows:
#!/usr/bin/perl
use strict;
use diagnostics;
use File::Find;
my $startdir = 'c:/iw/WA/';
my $doctype = 'mrk';
my $regex = m/\w\w\w\s(\d\d\d\d)/; (don't think I am doing this correctly)
my $date = '';
$date = $1;
print qq~Finding 099 yyyy and removing it from 099...\n~;
find(
sub{
return unless (/\.$doctype$/i);
local @ARGV = $_;
local $^I = '.bac';
while( <> ){
if(s/(\$o\d*)(\$yw\w\w*)/$1\$v$date$2/g){
print;
}
else {
print;
}
}
}, $startdir);
When I run the script I get the following error messages:
Use of uninitialized value in pattern match (m//) at c:\PERL\BIN\wattst5.pl
line 9 (#1)
(W uninitialized) An undefined value was used as if it were already
defined. It was interpreted as a "" or a 0, but maybe it was a mistake.
To suppress this warning assign a defined value to your variables.
To help you figure out what was undefined, perl tells you what operation
you used the undefined value in. Note, however, that perl optimizes your
program and the operation displayed in the warning may not necessarily
appear literally in your program. For example, "that $foo" is
usually optimized into "that " . $foo, and the warning will refer to
the concatenation (.) operator, even though there is no . in your
program.
Finding 099 yyyy and removing it from 099...
Im trying to capture the $1 of my m// in $regex as $date but I obviously don't
know enough about perl to get this to work. If I was doing a s/// on the same line of text
I would be fine, but because I am trying to do a match on one line, capture a group variable,
then move that value to another line in a replace s/// I am at a loss as to how to get that to work.
Included is a sample record to process:
=LDR 01103cas 2200313 a 4500
=001 cn\75033109\
=005 20070829142812.0
=008 760206c19669999oncur4p\\\\\\\0\\\a0eng\d
=010 \\$acn 75033109
=020 \\$a9781553677604 (pbk. : 84th ed. : Summer : 2008) :$c{dollar}120.00
=020 \\$a9781553677437 (pbk. : 83rd ed. : Spring : 2007)
=022 \\$a0317-9060
=040 \\$aCaOONL$beng$cCaOONL$dm.c.$dNST$dlbi$dlbi
=042 \\$anlc$aisds/c
=043 \\$an-cn---
=082 04$a343/.71/052$222
=099 \\$a336.2420971 CCH 2008
=110 2\$aCanada.
=222 \0$aCanadian income tax act with income tax regulations
=245 10$aCanadian income tax act with regulations.
=260 \\$aDon Mills, Ont.,$bCCH Canadian,$c
=300 \\$av. ;$c23 cm.
=362 0\$a35th ed.(1966)-
=500 \\$aDescription based on current issue.
=650 \0$aIncome tax$xLaw and legislation$zCanada.
=650 \0$aIncome tax$zCanada$xLaw.
=710 2\$aC C H Canadian Limited.
=780 10$aCanada. Laws, Statutes, etc.$t[Income tax act] Canadian income tax act.
=945 \1$h33420010942390$l120.00$o119$ywrqr
Any help would be greatly appreciated.